2

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.

0

1 Answer 1

3

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)) {}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.