没有用于初始化C++中的变量模板的匹配构造函数
No matching constructor for initialization of variadic template in C++
试图学习Variadic templates
,但不知道为什么它不会编译,错误是:CCD_ 2。
在我们讨论的时候,我想提出第二个问题,澄清我在代码中的意见。
template <typename ...Args> // this is just how Variadic template are defined
class test {
public:
int arr[sizeof...(Args)]; // this is packing or unpacking? why are
// dots outside of bracket?
test(Args... arg) // this is called packing?
: arr{arg...}{} // this is called un-packing?
};
int main(){
test<> t(1,2,3);
return 0;
}
编辑:似乎我需要做test <int, int, int>
,但为什么我必须这样做,因为另一个例子是这样工作的:
template <typename ...Args>
int func(Args... arg)
{
int a[] = {arg...};
return sizeof...(arg);
}
int main(void)
{
std::cout << func(1,2,3,4,5,6) << std::endl;
return 0;
}
func不需要<int, int ,int..
部分。
您的第一个示例不起作用,因为您需要指定以下类型
int main() {
test<int, int, int> t(1,2,3);
return 0;
}
如果你能使用C++17,你可以利用类模板的参数推导,并有这样的东西:
int main() {
test t(1,2,3);
return 0;
}
但在C++17之前,类模板参数推导不存在。但是,存在函数模板参数推导。这就是为什么您的第二个示例可以在不显式指定模板类型的情况下工作。
相关文章:
- 没有用于初始化C++中的变量模板的匹配构造函数
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 在类构造函数中使用结构变量
- 在 c++ 中将变量作为结构构造函数中的引用传递
- 在类构造函数中定义结构变量的参数
- 修改程序的入口点时未调用全局变量的构造函数
- C++:将向量传递到构造函数以创建成员变量的最佳方法?
- 为什么从另一个构造函数内部调用C++构造函数不修改类变量?
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- 构造函数干扰成员变量指定的初始值设定项?
- 类中的数组变量C++导致"was not declared in this scope"实现文件的一个函数中错误,但在构造函数中不会导致错误
- 通过 C++ 中的重载构造函数初始化未知类型的变量
- 如何在初始化列表中的构造函数之后初始化变量/对象?
- 我可以使用在类构造函数中初始化的流类型的成员变量吗?
- 为什么 std::move 不将默认移动构造函数中的源变量更改为默认值?
- C++为具有引用成员变量的类创建复制构造函数
- 如何在全局变量的构造函数之前运行函数
- C++/Win32 构造函数不使用从对话框获取的字符串初始化变量
- 将内置类型变量传递给只有一个类类型参数的"+"运算符函数时自动类型转换的构造函数
- std::线程构造函数(变量的数量)