Обходной путь для ошибки GCC 4.9 constexpr

У меня есть следующий кусок кода, который представляет собой большой кусок кода:

#include <iostream>
using namespace std;

template<size_t N> class A {
    public:
        static constexpr size_t getN() {return N;}
};

template<size_t N> class B {
    public:
        void print() { cout << "B created: " << N << '\n';}
};

template <class T> class C {
    public:
        void set(T* a) {
            t_ptr = a;
        }

        void create() {
            constexpr int m = t_ptr->getN();
            B<m> b;
            b.print();
        }

    private:
        T* t_ptr;
};

int main() {
   constexpr int n = 2;
   A<n> a;
   C<A<n> > c;
   c.set(&a);
   c.create();
}

Компилирование с g++ -o main main.cpp -std=c++11 и GCC/G++ 4.8.3 ожидаемый результат получен: B создано: 2

Однако с GCC/G++ 4.9.1 код не компилируется, вывод:

main.cpp: In member function ‘void C<T>::create()’:
main.cpp:27:15: error: the value of ‘m’ is not usable in a constant expression
             B<m> b;
               ^
main.cpp:26:27: note: ‘m’ used in its own initializer
             constexpr int m = t_ptr->getN();
                           ^
main.cpp:27:16: error: the value of ‘m’ is not usable in a constant expression
             B<m> b;
                ^
main.cpp:26:27: note: ‘m’ used in its own initializer
             constexpr int m = t_ptr->getN();
                           ^
main.cpp:27:19: error: invalid type in declaration before ‘;’ token
             B<m> b;
                   ^
main.cpp:28:15: error: request for member ‘print’ in ‘b’, which is of non-class type ‘int’
             b.print();
               ^

Это вызвано известной ошибкой в ​​GCC 4.9: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59937 и в этой старой ветке https://gcc.gnu.org/ml/gcc-bugs/2013-11/msg00067.html использование extern предлагается в качестве обходного пути. Однако я не могу заставить этот обходной путь работать.

Не могли бы вы, ребята, помочь мне скомпилировать этот код в GCC 4.9? Спасибо!

1 ответ

Решение

Поскольку this не является constexpr доступ к this->t_ptr тоже нет.

ошибка Clang немного более полезна

implicit use of 'this' pointer is only allowed within the
    evaluation of a call to a 'constexpr' member function

Ссылаясь на:

N3690 5.19 / 2 (выделение добавлено)

Условное выражение e является основным константным выражением, если при вычислении e, следуя правилам абстрактной машины, не будет выполнено одно из следующих выражений:

- это, за исключением функции constexpr или конструктора constexpr, который оценивается как часть e;

Вызов статической функции-члена через typename работает

constexpr int m = T::getN();
Другие вопросы по тегам