【发布时间】:2021-07-04 20:57:03
【问题描述】:
对于我的生活,我似乎无法创建这个类的临时,为什么不允许我这样做?
template <typename T>
struct Dog
{
Dog(T t) : tag(t) {}
T tag;
};
int main()
{
int tag = 6;
Dog<int>(6); // Works fine
Dog<int>(tag); // On Visual Studio I get no default constructor exists for class Dog<int>
// On onlineGDB I get error: no matching function for call to ‘Dog::Dog()’
}
另外,右值和左值有什么区别:
Dog(6); // Deduces fine
Dog(tag); // Deduction fail, error: missing template arguments before ‘(’ token
还有:
Dog<int>{tag}; // Works.
我接受它是因为这是聚合初始化?
【问题讨论】:
标签: c++ templates most-vexing-parse