Home Error with templates on gcc/clang but not on MSVC
Reply: 0

Error with templates on gcc/clang but not on MSVC

user1327
1#
user1327 Published in May 25, 2018, 8:56 am

I am having issues with templates and portability. Given this MVCE:

#include <cstdio>

class C
{
public:
    void go()
    {
        printf("go\n");
    }
};

template<typename T>
class A
{
public:
    A(T* pInstance) : m_pInstance(pInstance)
    {
    }

protected:
    T* m_pInstance;
};

template<typename T>
class B : public A<T>
{
    using Base = A<T>;
public:
    B(T* pInstance) : A<T>(pInstance)
    {
    }

    void foo()
    {
        B::m_pInstance->go();
        C* a = nullptr;
        if (a == &B::m_pInstance)
        {
        }

    }
};

int main(int argc, char** argv)
{
    C c;
    B<C> b(&c);
    b.foo();
}

I get the error:

main.cpp:37:9: error: invalid operands to binary expression ('C *' and 'C *A<C>::*')
                if (a == &B::m_pInstance)
                    ~ ^  ~~~~~~~~~~~~~~~
main.cpp:48:4: note: in instantiation of member function 'B<C>::foo' requested here
        b.foo();
          ^
1 error generated.

But I am not sure why I get this? Ok I see how the types are different but why would the latter being a member cause this problem? Visual Studio (which of course does have a different template engine) handles the same fine.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302334 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO