当我实例化模板类时,Visual Studio没有看到正确的构造函数。我在哪里弄错了?
我已经尝试过使复制/移动构造函数显式/删除。无济于事。
#include <set>
using namespace std;
template <class T, template<class> class ConnectionType>
struct node
{
T value;
node(const T& value) : value(value) {}
set<ConnectionType<T>> connections;
};
template <class T>
struct connection
{
node<T, connection>* n;
connection(node<T, connection>* n) :
n(n) {}
bool operator<(const connection& b) const
{
return n < b.n;
}
};
int main()
{
node<int, connection> a(0);
connection<int> c(&a); // ERROR HERE
return 0;
}
错误:
error C2664: 'connection<T>::connection(connection<T> &&)': cannot convert argument 1 from 'node<int, connection> *' to 'node<T, connection<T>> *'
在类模板的范围内,模板的名称实际上是“注入的类名称”,它的作用类似于类成员,并且可以用作模板名称或类型名称,表示使用中的专业化。([本地温度] / 1)
因此,当将此名称用作模板参数时,它可能表示含义,因此编译器需要检查相应的模板参数是类型还是模板。g ++和clang ++照原样接受您的代码。但是MSVC有一个错误,即使通常唯一相关的模板参数是模板模板参数,它也经常(但并非总是)假定用作模板参数的注入的类名称是类类型。(原始代码上的三个编译器:https : //godbolt.org/z/xrJSPB)
要解决此问题,您可以使用限定名称,例如::connection
当您从模板本身的作用域中指代模板的名称时。
template <class T>
struct connection
{
node<T, ::connection>* n;
connection(node<T, ::connection>* n) :
n(n) {}
bool operator<(const connection& b) const
{
return n < b.n;
}
};
(所有三个编译器都接受这一点:https : //godbolt.org/z/st7liP)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句