mercredi 2 août 2017

How to use make_tuple() in static constexpr class member?

I am trying to use make_tuple() in constexpr. It works in global scope. But it generates link error for static constexpr class member.

#include <iostream>
#include <tuple>

using namespace std;

class A
{
public:
    static constexpr auto z = make_tuple(5, 3.0);
};

constexpr auto tp = make_tuple(6, 3.2);

int main()
{

    cout << get<0>(tp) << " " << get<1>(tp) << endl; // OK

    cout << get<0>(A::z) << "  " << get<1>(A::z) << endl; // error: (.text+0x5a): undefined reference to `A::z'
                                                          //        (.text+0x67): undefined reference to `A::z'

}

I have checked here make_tuple is not itself a constexpr in c++11. I guess that is not the problem in this case. If it was it would generate a compile error instead of link error.

I have tried to define the constexpr outside the class like bellow as suggested by this answer

class A
{
public:
    static constexpr tuple<int, double> z;
};

constexpr tuple<int, double> A::z = make_tuple(5, 3.0);

But, it generates several compile error. That makes sanse according to answers of constexpr initializing static member using static function

What is the correct way to use make_tuple in static constexpr class member?

Compiler: g++ 4.8.4 and clang 3.4 with -std=c++11

Aucun commentaire:

Enregistrer un commentaire