Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disambiguate nested class and member function with same name in template

See the example: compilable here

// Example program
#include <iostream>
#include <string>

class Foo {
    public:
    
    class Nes{};

    void Nes(){}

    using Ptr = void(Nes::*)();
 };

 template<typename T> class Bar
 {
 public:

     using Ptr = void(T::Nes::*)();
 };

int main()
{
    using F = void(Foo::Nes::*)();
    F compiles;

    Bar<Foo>::Ptr notCompiles;

    std::cout << typeid(Foo).name();
}

How can tell to the compiler that Nes should be the class and not the method?

like image 722
Luca Avatar asked Oct 24 '25 16:10

Luca


1 Answers

In this case:

template<typename T> class Bar {
public:
    using Ptr = typename T::Ptr;
};

If T doesn't have a Ptr type definition:

template <typename T> class Bar {
public:
    using TNes = class T::Nes;
    using Ptr = void (TNes::*)();
};
like image 173
Ted Lyngmo Avatar answered Oct 26 '25 04:10

Ted Lyngmo



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!