Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22006

Different behavior between g++ and clang++ in nested classes

$
0
0

I noticed a different behavior between gcc 9.2.0 and clang++ 9.0.1. My code is as follows

//header.hh
     ...
template <typename T>
class Outer {
     ...
  public:
     template <typename S>
     class Inner;
     ... 
};

template <typename T>
template <typename S>
class Inner {
     ...
      Inner& func();
     ...
};

then, since the function func() is implemented in another file

//implementation.cc
template <typename T>
template <typename S>
Outer<T>::Inner<S>& Outer<T>::Inner<S>::func() {
    ...
};

Now, if I use g++ the compilation is OK. If I use clang++ I get

src/implementation.cc:6:1: error: missing 'typename' prior to dependent type template name 'Outer<T>::Inner'
Outer<T>::Inner<S>& Outer<T>::Inner<S>::func() {
^
1 error generated.

However if I follow its suggestion and use

typename Outer<T>::Inner<S>& Outer<T>::Inner<S>::func()

I got another error:

src/implementation.cc:6:21: error: use 'template' keyword to treat 'Inner' as
a dependent template name typename Outer<T>::Inner<S>& Outer<T>
::Inner<S>::func() {

And now its suggestion seems very weird.

QUESTIONS

  1. Why the two compiler are behaving differently?
  2. What is the correct syntax to use?

Viewing all articles
Browse latest Browse all 22006

Trending Articles