Ivan
Ivan

Reputation: 7746

if constexpr usage for variable length element Get<>

I am trying to get the second element of the list, but I get an error:

    ||=== Build: Debug in hellocpp17 (compiler: GNU GCC Compiler) ===|
/home/idf/Documents/c++/hellocpp17/main.cpp||In function ‘int main()’:|
/home/idf/Documents/c++/hellocpp17/main.cpp|67|error: no matching function for call to ‘Get<2>::Get(std::__cxx11::list<unsigned int>&)’|
/home/idf/Documents/c++/hellocpp17/main.cpp|40|note: candidate: constexpr Get<2>::Get()|
/home/idf/Documents/c++/hellocpp17/main.cpp|40|note:   candidate expects 0 arguments, 1 provided|
/home/idf/Documents/c++/hellocpp17/main.cpp|40|note: candidate: constexpr Get<2>::Get(const Get<2>&)|
/home/idf/Documents/c++/hellocpp17/main.cpp|40|note:   no known conversion for argument 1 from ‘std::__cxx11::list<unsigned int>’ to ‘const Get<2>&’|
/home/idf/Documents/c++/hellocpp17/main.cpp|40|note: candidate: constexpr Get<2>::Get(Get<2>&&)|
/home/idf/Documents/c++/hellocpp17/main.cpp|40|note:   no known conversion for argument 1 from ‘std::__cxx11::list<unsigned int>’ to ‘Get<2>&&’|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Program:

#include <iostream>
#include <algorithm>
#include <list>

using namespace std;

template<unsigned n>
struct Get
{
    template<class X, class...Xs>
    constexpr auto operator()(X x, Xs...xs)
    {
        if constexpr(n > sizeof...(xs) )
        {
            return;
        }
        else if constexpr(n > 0)
        {
            return Get<n-1> {}(xs...);
        }
        else
        {
            return x;
        }
    }
};



int main()
{
    list<unsigned> l = { 7, 5, 16, 8 };
    unsigned l2 = Get<2>(l);

    cout << l2 << endl;

    return 0;
}

EDIT 1

If I instantiate a Get<2>, this error is reported by the compiler

unsigned l2 = Get<2>()(l);

/home/idf/Documents/c++/hellocpp17/main.cpp|67|error: void value not ignored as it ought to be|

Upvotes: 0

Views: 99

Answers (1)

max66
max66

Reputation: 66200

You can try with

unsigned l2 = Get<2>{}(7, 5, 16, 8);

The first problem in your code is that

Get<2>(l);

isn't a call to operator() of Get<2>; it's a construction of a Get<2> object with a std::list parameter.

Unfortunately there isn't a Get<2> constructor that receive a std::list.

The second problem in your code is that if you call the operator() of Get<2> as you think

Get<2>{}(l)

where l is a std::list, you pass a single argument; not a variadic list of arguments. And you can use the list l only run-time, not compile time as you want.

Unfortunately the Get<2>{}(7, 5, 16, 8) way (the operator() that receive a variadic list of arguments) is incompatible with a variable that contain a list.

I mean... you can't do something as follows

auto l = something{7, 5, 16, 8};

Get<2>{}(l);

But, if you modify the operator() to receive a std::integer_sequence as follows

template <template <typename X, X...> class C,
          typename T, T I0, T ... Is>
constexpr auto operator() (C<T, I0, Is...> const &)
 {
   if constexpr (n > sizeof...(Is) )
      return;
   else if constexpr (n > 0)
      return Get<n-1>{}(C<T, Is...>{});
   else
      return I0;
 }

you can pass through a l variable as follows

auto l { std::integer_sequence<int, 7, 5, 16, 8>{} };

unsigned l2 = Get<2>{}(l);

Upvotes: 3

Related Questions