11

I ran into this problem in some real-life C++11 code, but I boiled it down to this:

template<int i> struct Dummy {};

template<typename T>
struct Foo {
  template<int i> static constexpr int bar() { return i; }

  template<int i>
  static auto working() -> Dummy<bar<i>()>;

  template<int i>
  static auto also_working() -> Dummy<Foo<T>::template bar<i>()>;

  template<int i>
  static Dummy<Foo<T>::template bar<i>()> not_working();
};

template<typename T> template<int i>
auto Foo<T>::working() -> Dummy<bar<i>()> {
  return Dummy<bar<i>()>{};
}

template<typename T> template<int i>
auto Foo<T>::also_working() -> Dummy<Foo<T>::template bar<i>()> {
  return Dummy<bar<i>()>{};
}

template<typename T> template<int i>
Dummy<Foo<T>::template bar<i>()> Foo<T>::not_working() {
  return Dummy<bar<i>()>{};
}

I was trying to create an out-of-line definition of a template member function of a template class, where the signature of the function involved calling another template member function and started out with something like the not_working() function. The problem was that the definition failed to match the declaration.

Clang said:

clang++ -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded -std=c++11   -c -o out_of_line.o out_of_line.cc
out_of_line.cc:28:42: error: out-of-line definition of 'not_working' does not match any declaration in 'Foo<T>'
Dummy<Foo<T>::template bar<i>()> Foo<T>::not_working() {
                                         ^~~~~~~~~~~

GCC said:

g++ -Wall -Wextra -pedantic -std=c++11   -c -o out_of_line.o out_of_line.cc
out_of_line.cc:28:34: error: prototype for ‘Dummy<bar<i>()> Foo<T>::not_working()’ does not match any in class ‘Foo<T>’
 Dummy<Foo<T>::template bar<i>()> Foo<T>::not_working() {
                                  ^~~~~~
out_of_line.cc:14:43: error: candidate is: template<class T> template<int i> static Dummy<Foo<T>::bar<i>()> Foo<T>::not_working()
   static Dummy<Foo<T>::template bar<i>()> not_working();
                                       ^~~~~~~~~~~

By trial and error I discovered that using a trailing return type I could get the definition to match the declaration, yielding the also_working() function. Once there I realized that due to the change of scope in the trailing return type I could do away with some name qualification, yielding the much prettier working() function.

Now I wonder why the not_working() function is not working, i.e. why its definition does not match its declaration (I could just settle in ignorance with the solution I found, but I'm likely to encounter more of this kind of issues and I don't want to waste more time using trial and error); does the bug lie within the compilers or within my code. I've read through 14.6 Name resolution [temp.res] but I'm not sure what rules are applicable for this case.

Clarification of the question: Given the rules in the C++11 standard:

  1. should the not_working() definition match the declaration?
  2. which rules are involved in determining 1.?
  3. how do the rules from 2. interact when determining 1.?
3
  • Strange but true... The same code using C++14 variable templates compiles fine with g++, but still doesn't compile with clang++. Looks like a compiler bug for me. Mar 14, 2017 at 19:36
  • While not addressing this particular issue, this core language issue contains an answer suggesting that the intention is to let out-of-line definitions match declarations with textually matching signatures.
    – psyill
    Mar 15, 2017 at 9:40
  • One of those places where auto helps with types you can't write. gcc and clang are both happy with auto Foo<T>::working(); as far as I can tell, and will deduce the return type when they need it.
    – TBBle
    May 10, 2017 at 17:03

1 Answer 1

0

It looks like it's trying to implement CWG2 but is possibly doing things in a surprising order. Looking at gcc's errors:

prog.cc:28:34: error: prototype for 'Dummy<bar<i>()> Foo<T>::not_working()' does not match any in class 'Foo<T>'
 Dummy<Foo<T>::template bar<i>()> Foo<T>::not_working() {
                                  ^~~~~~
prog.cc:14:43: error: candidate is: template<class T> template<int i> static Dummy<Foo<T>::bar<i>()> Foo<T>::not_working()
   static Dummy<Foo<T>::template bar<i>()> not_working();
                                           ^~~~~~~~~~~

The definition is seen with the return type Dummy<bar<i>()> but the candidate declaration has return type Dummy<Foo<T>::bar<i>()>. Specifically, the Foo<T>:: qualification on bar<i> has been lost.

Changing the definition of also_working to have return type Dummy<Foo<T>::template bar<2>()>, we get useful parallel errors:

prog.cc:23:6: error: prototype for 'Dummy<Foo<T>::bar<2>()> Foo<T>::also_working()' does not match any in class 'Foo<T>'
 auto Foo<T>::also_working() -> Dummy<Foo<T>::template bar<2>()> {
      ^~~~~~
prog.cc:11:15: error: candidate is: template<class T> template<int i> static Dummy<Foo<T>::bar<i>()> Foo<T>::also_working()
   static auto also_working() -> Dummy<Foo<T>::template bar<i>()>;
               ^~~~~~~~~~~~

Here the definition is seen with the return type Dummy<Foo<T>::bar<2>()> (as written), and the candidate declaration has return type Dummy<Foo<T>::bar<i>()>.

Clearly, Foo<T>::bar<i> is different from bar<i> even in the context of Foo<T>, since removing Foo<T>::template out of either the declaration or definition of the return type of also_working makes it stop working. (Taking both out gets you back working.)

I tried changing the declaration of not_working to be:

  template<int i>
  static Dummy<bar<i>()> not_working();

and now gcc complains:

prog.cc:28:34: error: prototype for 'Dummy<bar<i>()> Foo<T>::not_working()' does not match any in class 'Foo<T>'
 Dummy<Foo<T>::template bar<i>()> Foo<T>::not_working() {
                                  ^~~~~~
prog.cc:14:26: error: candidate is: template<class T> template<int i> static Dummy<bar<i>()> Foo<T>::not_working()
   static Dummy<bar<i>()> not_working();
                          ^~~~~~~~~~~

Which is pretty clearly nonsensical, since we have character-for-character-comparable declaration and definition Dummy<bar<i>()> Foo<T>::not_working(), once the compiler's done with it.

2
  • Thanks for taking a bout with GCC, but IMHO the fact that a particular compiler is failing at matching the definition with the declaration does not answer the question. I was looking for the particular rules involved, telling me if the definition should match the declaration according to the standard regardless of the compiler used. Re-reading my question I see that it's apparently too vague: I'll update it to be more specific. Sorry if my vagueness misled you.
    – psyill
    May 15, 2017 at 19:38
  • Well, you already have that answer in the comments, that the ordering is currently vague in the specification, and CWG issue 2 is intended to fix that, but has not yet been worded and submitted. What I was trying to answer is why it doesn't work. It happens that gcc gives us clues to what it's trying to match, while clang is just saying "Didn't match".
    – TBBle
    May 16, 2017 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.