在 Ubuntu 14.04 上使用 GCC v4.8 在 C++11 中定义元组向量时出现编译错误

Compilation Errors while defining vector of tuple in C++11 using GCC v4.8 on Ubuntu 14.04

我正在尝试在 C++ 11 中定义一个元组向量,如下所示:

#include <iostream>
#include <string>
#include <vector>
#include <tuple>

typedef unsigned char uchar;
typedef std::tuple<uchar, std::string, uchar, float> fruitInfoTuple;
const std::vector<fruitInfoTuple> jointsInfo{
  { 0,  "mango",   100,   -6.01},
  {10,  "apple",   144,    6.25},
  {12,  "orange",  159,    2.59},
  {33,  "banana",  144,  -28.96},
  { 4,  "grapes",  128,    3.79},
};

我在启用 C++11 标志的情况下编译程序。但是,它显示如下所示的复杂错误:

ravi@lab:~/Desktop/a$ g++  -std=c++11 learn.cpp 
learn.cpp:14:1: error: converting to ‘std::tuple<unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float>’ from initializer list would use explicit constructor ‘constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {int, const char (&)[6], int, double}; <template-parameter-2-2> = void; _Elements = {unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float}]’
 };
 ^
learn.cpp:14:1: error: converting to ‘std::tuple<unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float>’ from initializer list would use explicit constructor ‘constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {int, const char (&)[6], int, double}; <template-parameter-2-2> = void; _Elements = {unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float}]’
learn.cpp:14:1: error: converting to ‘std::tuple<unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float>’ from initializer list would use explicit constructor ‘constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {int, const char (&)[7], int, double}; <template-parameter-2-2> = void; _Elements = {unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float}]’
learn.cpp:14:1: error: converting to ‘std::tuple<unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float>’ from initializer list would use explicit constructor ‘constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {int, const char (&)[7], int, double}; <template-parameter-2-2> = void; _Elements = {unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float}]’
learn.cpp:14:1: error: converting to ‘std::tuple<unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float>’ from initializer list would use explicit constructor ‘constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {int, const char (&)[7], int, double}; <template-parameter-2-2> = void; _Elements = {unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float}]’

我猜 GCC 4.8 不支持元组功能。请问有什么解决方法吗?请注意,如果需要,我可以使用 boost。我只想要一种像上面那样定义元组的干净方法。

你可以试试:

const std::vector<fruitInfoTuple> jointsInfo{
    fruitInfoTuple{ 0,  "mango",   100,   -6.01},
    fruitInfoTuple{10,  "apple",   144,    6.25},
    fruitInfoTuple{12,  "orange",  159,    2.59},
    fruitInfoTuple{33,  "banana",  144,  -28.96},
    fruitInfoTuple{ 4,  "grapes",  128,    3.79},
};

在 C++11 中,您应该使用 std::make_tuple 构造元组:

#include <iostream>
#include <string>
#include <vector>
#include <tuple>

typedef unsigned char uchar;
typedef std::tuple<uchar, std::string, uchar, float> fruitInfoTuple;
const std::vector<fruitInfoTuple> jointsInfo{
  std::make_tuple( 0,  "mango",   100,   -6.01),
  std::make_tuple(10,  "apple",   144,    6.25),
  std::make_tuple(12,  "orange",  159,    2.59),
  std::make_tuple(33,  "banana",  144,  -28.96),
  std::make_tuple( 4,  "grapes",  128,    3.79),
};
int main()
{
    for(int i = 0 ; i < jointsInfo.size(); ++i)
    {
        std::cout << std::get<1>(jointsInfo[i]) << std::endl;

    }
}

结果:

mango
apple
orange
banana
grapes