1

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?

Heinzi
  • 4,834
  • 3
  • 31
  • 57
  • 1
    I think you need to use the keyword `template` because the compiler isn't permitted to understand that `A::func` is a template. (that explains your non-working case, so I don't understand your working case). – JSF Oct 27 '15 at 16:12
  • 1
    http://stackoverflow.com/questions/8463368/template-dot-template-construction-usage – JSF Oct 27 '15 at 16:16
  • 1
    The one I just linked directs you to the "accepted" answer http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords in which I find it harder to find the use of `template` right after `.` but either of those links contains your answer – JSF Oct 27 '15 at 16:20
  • thanks, that works. If you post it as an answer, I'll accept it. – Heinzi Oct 27 '15 at 16:30

0 Answers0