Gcc 4.5.1 vs. partial specialization of the VC2010 template: what is C ++ 0x?

The following compilations are with VC2010, but not with gcc 4.5.1 with -std = C ++ 0x:

template <class T, class TBase>
class TestBase : public TBase
{
public:
// Helper functor
    template <unsigned int t_u, class TRet = int>
    struct Helper
    {
        TRet operator() (int x = 0, int y = 0)
        {
                return (TRet)t_u;
        }
    };
}; // class TestBase

template<class TBase>
class Test0 : public TestBase<Test0<TBase>, TBase>
{
public:
    Helper<100> Get100; // gcc 4.5.1 error: 'Helper' does not name a type
};

gcc accepts a more detailed description:

template<class TBase>
class Test1 : public TestBase<Test1<TBase>, TBase>
{
    typedef TestBase<Test1<TBase>, TBase> thisBase;
public:
    typename thisBase::template Helper<100> Get100;
};

Which (if any) is C ++ 0x compatible?

+3
source share
1 answer

In the first code snippet, Helper is not a dependent name. Since it is independent, it should be allowed when analyzing the template, and not when creating it. This is necessary in both C ++ 03 and C ++ 0x.

Using typedef makes it a dependent name, so it is allowed when instantiating. You can also use typename Test1 :: template Helper <100>.

+2
source

Source: https://habr.com/ru/post/1793352/


All Articles