user2649502
user2649502

Reputation: 37

GCC 4.4.7 cannot convert from initializer list to std::array

I'm trying to put the top answer from this question into my own project, and am having trouble getting gcc to compile it (on RHEL linux using "-std=gnu++0x"). I have already made a few changes to that original code, such that mine ends up as follows:

#include <string>
#include <vector>
#include <fstream>
#include <cerrno>
#include <cstring>
#include <array>
#include <algorithm>

template <typename T0, typename T1, size_t N>   bool operator *(const T0& lhs, const std::array<T1, N>& rhs)
{
    return std::find(rhs.begin(), rhs.end(), lhs) != rhs.end();
}
template <class T0, class...T>  std::array<T0, 1+sizeof...(T)> in(T0 arg0, T...args)
{
    return {{arg0, args...}};
}

Some of those includes will not be relevant, I'm just showing all of them for you to see. I am using the functions like this:

if (1 *in(1,2,3))

When compiling, gcc gives the following error on the "return" line of "in":

 error: could not convert '{{arg0, args#0, args#1}}' to 'std::array<int, 3u>'

Can anyone shed any light on why this is please?

I haven't done much in C++11 before, so I'm a bit lost in trying to find out what's wrong. I have already tried with different numbers of {} around the "args" bit, but to no avail so far.

Thanks for any help you can give.

Upvotes: 2

Views: 1147

Answers (1)

Captain Obvlious
Captain Obvlious

Reputation: 20073

The problem is that you are using an old version of GCC that doesn't have complete support for C++11. Update to GCC 4.8.x and your code will compile just fine.

A live version of the code below can be found on ideone

#include <array>
#include <algorithm>

template <typename T0, typename T1, size_t N>
bool operator *(const T0& lhs, const std::array<T1, N>& rhs)
{
    return std::find(rhs.begin(), rhs.end(), lhs) != rhs.end();
}
template <class T0, class...T>
std::array<T0, 1 + sizeof...(T)> in(T0 arg0, T...args)
{
    return { {arg0, args...} };
}

int main()
{
    if (1 * in(1, 2, 3)) {}
}

Upvotes: 3

Related Questions