HC4*_*ica 10 c++ copy-constructor initializer-list uniform-initialization c++11
我对以下编译器错误感到惊讶:
template <typename T>
struct A
{
    A(T t): t_{t} {}
    T t_;
};
struct S
{
};
int main()
{
    A<S> s{S{}};
}
错误是(与clang):
test.cpp:4:16: error: excess elements in struct initializer
    A(T t): t_{t} {}
               ^
test.cpp:15:10: note: in instantiation of member function 'A<S>::A' requested here
    A<S> s{S{}};
         ^
GCC给出了类似的错误.
我希望表达t_{t},试图复制构建t_从t.由于S有一个隐式生成的复制构造函数,我不希望这是一个问题.
有人能解释一下这里发生了什么吗?
Nic*_*las 17
S可能有一个隐式生成的复制构造函数,但S也是其他东西.一个汇总.因此,(几乎)任何使用都{}将对其执行聚合初始化.因此,{}预计内容将是聚合成员的值.因为你的总量是空的......繁荣.
在模板代码中,出于这些原因,应该避免使用统一初始化语法.对于未知类型T,您无法确定{...}将要做什么.
| 归档时间: | 
 | 
| 查看次数: | 3806 次 | 
| 最近记录: |