在下面的代码中,我不允许声明显式ctor,因为编译器说我在复制初始化上下文(clang 3.3 和 gcc 4.8)中使用它。我试图通过使 ctor 不显式然后将复制构造函数声明为已删除来证明编译器是错误的。
编译器错了还是有其他解释?
#include <iostream>
template <typename T>
struct xyz
{
constexpr xyz (xyz const &) = delete;
constexpr xyz (xyz &&) = delete;
xyz & operator = (xyz const &) = delete;
xyz & operator = (xyz &&) = delete;
T i;
/*explicit*/ constexpr xyz (T i): i(i) { }
};
template <typename T>
xyz<T> make_xyz (T && i)
{
return {std::forward<T>(i)};
}
int main ()
{
//auto && x = make_xyz(7);
auto && x (make_xyz(7)); // compiler sees copy-initialization here too
std::cout << x.i << std::endl;
}
更新一个不切实际但简单得多的版本
struct xyz {
constexpr xyz (xyz const &) = delete;
constexpr xyz (xyz &&) = delete;
xyz & operator = (xyz const &) = delete;
xyz & operator = (xyz &&) = delete;
int i;
explicit constexpr xyz (int i): i(i) { }
};
xyz make_xyz (int && i) {
return {i};
}
int main () {
xyz && x = make_xyz(7);
}