mardi 27 octobre 2015

Pass numeric template parameter to template function causes compiler error [duplicate]

This question already has an answer here:

I'm using gcc 5.2.1 on Ubuntu 15.10 64bit. Forwarding a size_t parameter to a function call causes a compiler error.

The following code does not compile:

#include <cstdlib>

template<size_t SIZE>
struct A {
  template<size_t SIZE2> void func() const {}
};

template<class T> void do_something() {
  constexpr size_t SIZE = T::SIZE; // Replace this line to make it working
  //constexpr size_t SIZE = 5; // If this line is used instead, it works.
  A<SIZE> obj;
  obj.func<1>();
}

Here the compiler output:

$ g++ main.cpp --std=c++14
main.cpp: In function ‘void do_something()’:
main.cpp:12:15: error: expected primary-expression before ‘)’ token
   obj.func<1>();
               ^

However, if I replace the marked line with the alternative one, it compiles fine.

What is the reason for this? Is this a compiler bug? I reckon the obj.func<1>() call should only depend on the type of the SIZE variable, not on how it is initialized.

Am I doing something wrong? Any ideas on a workaround?

Aucun commentaire:

Enregistrer un commentaire