类模板参数推导因派生类而失败 [英] Class template argument deduction failed with derived class
本文介绍了类模板参数推导因派生类而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <utility>
template<class T1, class T2>
struct mypair : std::pair<T1, T2>
{ using std::pair<T1, T2>::pair; };
int main()
{
(void)std::pair(2, 3); // It works
(void)mypair(2, 3); // It doesn't work
}
以上格式是否正确?
如果构造函数被继承,是否可以在第二种情况下推断类模板参数? std :: pair
的构造函数是否正在参与 mypair
的隐式扣除指南的创建?
Is it possible deduce the class template arguments in the second case if the constructors are being inherited? Are the constructors of std::pair
participating in the creation of implicit deduction guides for mypair
?
我的编译器是g ++ 7.2.0。
My compiler is g++ 7.2.0.
推荐答案
请参见 Richard Smith的答案。
A此答案的先前版本已说明以下应该应该起作用
A previous version of this answer had stated that the following should work
template <class T> struct B { B(T ) { } };
template <class T> struct D : B<T> { using B<T>::B; };
B b = 4; // okay, obviously
D d = 4; // expected: okay
但这并不是真正可行的方法,甚至都不是一个好方法像我想的那样工作的想法(我们继承了构造函数,但没有继承推论指南?)
But this isn't really viable, and wouldn't even be a good idea to work as I thought it would (we inherit the constructors but not the deduction guides?)
这篇关于类模板参数推导因派生类而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文