混淆在ISO C ++标准中的std :: tuple的默认构造函数描述 [英] Confused by default constructor description of std::tuple in the ISO C++ Standard
问题描述
标准表示 std :: tuple
有以下成员函数
The Standard says that std::tuple
has the following member functions
constexpr tuple();
explicit tuple(const Types&...);
有人可以解释一下 std :: tuple& >
?
推荐答案
我认为这是标准中的一个小错误。显然,当 Types
参数包为空时,两个构造函数调用是等效的,不能重载(参见C ++ 11第13节)。 (进一步注意,使用 Types
的构造函数不是一个成员模板 - 如果是,那么它将是一个合法的重载。)
I believe this is a minor error in the standard. Clearly, when the Types
parameter pack is empty, the two constructor calls are equivalent and cannot be overloaded (see C++11 section 13). (Further note that the constructor using Types
is not a member template either --if it was, then it would be a legal overload.).
换句话说,这段代码不会编译:
In other words, this code will not compile:
template <typename... Types>
struct Test
{
constexpr Test() {}
explicit Test(Types const&...) { /* etc. */ }
};
int main()
{
Test<> a;
Test<int> b;
}
例如,g ++ v4.8快照输出:
e.g., a g++ v4.8 snapshot outputs:
tt.cxx: In instantiation of ‘struct Test<>’:
tt.cxx:10:10: required from here
tt.cxx:5:12: error: ‘Test<Types>::Test(const Types& ...) [with Types = {}]’ cannot be overloaded
explicit Test(Types const&...) { /* etc. */ }
^
tt.cxx:4:13: error: with ‘constexpr Test<Types>::Test() [with Types = {}]’
constexpr Test() {}
^
部分专业化:
template <typename... Types>
struct Test
{
constexpr Test() {} // default construct all elements
explicit Test(Types const&...) { /* etc. */ }
// and all other member definitions
};
template <>
struct Test<>
{
constexpr Test() {}
// and any other member definitions that make sense with no types
};
int main()
{
Test<> a;
Test<int> b;
}
这将正确编译。
看起来标准想要 constexpr
默认构造函数是 std :: tuple< var;
可以写成 std :: tuple<> var();
或 std :: tuple<> var {};
,因为显式
与其他构造函数一起使用。不幸的是,它的定义 std :: tuple
不适用于大小为零的元组。该标准允许在第20.4.2.7节(关系运算符)中使用对于任何两个零长度元组[...]。糟糕! : - )
It appears the standard wanted a constexpr
default constructor was so that std::tuple<> var;
could be written instead of writing std::tuple<> var();
or std::tuple<> var{};
because of the use of explicit
with the other constructor. Unfortunately, its definition of std::tuple
does not work for tuples of size zero. The standard does permit such in section 20.4.2.7 (relational operators) though, "For any two zero-length tuples, [...]". Oops! :-)
这篇关于混淆在ISO C ++标准中的std :: tuple的默认构造函数描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!