[英]error C2783: could not deduce template argument for structure argument
I am facing error C2783. 我面临错误C2783。 I reproduce error with similar structure test case. 我用类似结构的测试用例重现错误。
Here is test case: 这是测试用例:
#include <iostream>
namespace ns1 {
template <class T> class class_1 {};
}
namespace ns2 {
using namespace ns1;
template <typename T> inline ns1::class_1<T> myfunc();
template<typename T>
inline ns1::class_1<T> myfunc() {
int a,b;
std::cin>>a;
std::cin>>b;
if(a<b) return true;
else return false;
}
}
namespace ns3 {
struct myStruct {
ns1::class_1<double> var1;
ns1::class_1<double> var2;
myStruct ( const ns1::class_1<double>& cl0= ns2::myfunc<double>(),
const ns1::class_1<double>& cl1= ns2::myfunc<double>()): var1(cl0), var2(cl1) {};
};
}
Error is : 错误是:
error C2783: 'ns1::class_1 ns2::myfunc(void)' : could not deduce template argument for 'T' 错误C2783:'ns1 :: class_1 ns2 :: myfunc(void)':无法推断出'T'的模板参数
Also i wonder why its giving error for line 27 (cl0) but not line 28 (for cl1)? 我也想知道为什么它给27行(cl0)而不是28行(cl1)的错误? If I try to use this on some function its works fine only giving error when using in structure arguments. 如果我尝试在某些函数上使用它,则只能在结构参数中使用时给出错误。
This is a compiler bug. 这是一个编译器错误。 If you replace the contents of myfunc
with valid code (as suggested), it still doesn't work. 如果使用有效代码替换myfunc
的内容(如建议的那样),它将仍然不起作用。 For a description, status (and acknowledgement) of the bug, see Microsoft Connect . 有关错误的描述,状态(和确认),请参阅Microsoft Connect 。 You might try to use a helper type to get argument deduction (which works): 您可以尝试使用辅助类型来进行参数推导(有效):
namespace ns1 {
template <class T> class class_1 {
public: class_1 (int a, int b){}
};
}
namespace ns2 {
template<class T> struct deduction_helper{};
using namespace ns1;
template <typename T> inline ns1::class_1<T> myfunc(deduction_helper<T>);
template<typename T>
inline ns1::class_1<T> myfunc(deduction_helper<T>) {
int a,b;
std::cin>>a;
std::cin>>b;
ns1::class_1<T> c(a,b); return c;
}
}
namespace ns3 {
struct myStruct {
ns1::class_1<double> var1;
ns1::class_1<double> var2;
myStruct ( const ns1::class_1<double>& cl0= ns2::myfunc(ns2::deduction_helper<double>()),
const ns1::class_1<double>& cl1= ns2::myfunc(ns2::deduction_helper<double>())
): var1(cl0), var2(cl1) {};
};
}
int main()
{
ns3::myStruct x;
}
NB as the helper type resides in ns2
, you could use ADL instead of qualifying the name myfunc
. 注意,因为辅助程序类型位于ns2
,所以可以使用ADL代替限定名称myfunc
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.