Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependent names in C++ template

Tags:

c++

templates

I have templated class that includes two nested class like below

#include <iostream>

template<typename A>
class c
{
    class a
    {
        protected:
        int i = 0;
    };
    class b: public a
    {
        public:
        void print()
        {
            std::cout << "i of a: " << i; // fails
            //std::cout << "i of a: " << this->i; // works
        }
    };
};

This gives compliation error: use of undeclared identifier 'i'. I would expect the failure to relate to class a being dependent on the template parameter, but it does not seem to be the case here since a is not dependent on A. I tried reading about dependent names in templates, but I am still unsure why this error occurs. Could someone explain the theoretical reason for this behavior? Surprisingly this compiles without issues on MSVC but not in GCC and Clang. Any insights on why GCC/Clang behave differently from MSVC here would be greatly appreciated!

like image 932
Ashiful Avatar asked Oct 19 '25 02:10

Ashiful


1 Answers

since a is not dependent on A

It is. You can specialize a.

#include <iostream>

template<typename A>
class c
{
  public:
    class a
    {
      protected:
        int i = 0;
    };

    class b: public a
    {
      public:
        void print()
        {
            std::cout << "i of a: " << this->i;
        }
    };
};

template <>
class c<int>::a
{
    // No `i`!
};

int main()
{
    c<int>::b x;
    x.print();
}
<source>:18:46: error: no member named 'i' in 'c<int>::b'
   18 |             std::cout << "i of a: " << this->i; // works
      |                                        ~~~~  ^

Surprisingly this compiles without issues on MSVC

You didn't enable the standard conformance mode. All modern compilers will accept some invalid C++ by default.

It starts rejecting your code with /std:c++latest or with /permissive-.

like image 107
HolyBlackCat Avatar answered Oct 21 '25 16:10

HolyBlackCat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!