我正在尝试元组并遇到创建元组的问题.代码示例如下.
//a.cpp
#include <tuple>
using namespace std;
int main() {
auto te = make_tuple(); //this line is ok
auto tte = make_tuple(te); //this line gives an error.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我用g ++ 4.5(g ++ -std = c ++ 0x a.cpp)和MS VC++ 2010编译它.两个编译器都在main()的第二行给出了一个错误.
我的问题是:由于'te'是一个定义明确的变量,为什么不能用te作为内容来创建另一个元组.这个语义是否正确?
我想这是一种边界情况,但如果算法是正确的,那么应该允许零,恕我直言.
仅供参考,来自gcc的错误消息是:
$ gcc -std=c++0x a.cpp
In file included from a.cpp:1:0:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/tuple: In constructor
'std::tuple<_Elements>::tuple(std::tuple<_UElements ...>&) [with _UElements = {},
_Elements = {std::tuple<>}]':
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/tuple:551:62: instantiated from
'std::tuple<typename std::__decay_and_strip<_Elements>::__type ...>
std::make_tuple(_Elements&& ...) [with _Elements = {std::tuple<>&}, typename
std::__decay_and_strip<_Elements>::__type …Run Code Online (Sandbox Code Playgroud) 我的基本想法是从std :: tuple派生我自己的类,以获得一些内部帮助器类型:
template <typename ... T>
class TypeContainer: public std::tuple<T...>
{
public:
using BaseType = std::tuple<T...>;
static const size_t Size = sizeof...(T);
TypeContainer(T... args):std::tuple<T...>(args...){};
using index_sequence = std::index_sequence_for<T...>;
};
Run Code Online (Sandbox Code Playgroud)
现在我尝试使用如下代码:
using MyType_tuple_with_empty = std::tuple< std::tuple<float,int>, std::tuple<>, std::tuple<int>>;
using MyType_typecontainer_with_empty = TypeContainer< TypeContainer<float,int>, TypeContainer<>, TypeContainer<int>>;
using MyType_tuple_non_empty = std::tuple< std::tuple<float,int>, std::tuple<int>, std::tuple<int>>;
using MyType_typecontainer_non_empty = TypeContainer< TypeContainer<float,int>, TypeContainer<int>, TypeContainer<int>>;
template <typename T>
void Do( const T& parms )
{
// The following lines result in errors if TypeContainer with
// …Run Code Online (Sandbox Code Playgroud) 在最近关于boost开发者邮件列表的邮件中,Eric Niebler提到可以在O(1)实例化中获取模板参数包的最后一个元素.
如何才能做到这一点?