Private class specialization error

At compile time, the code below in clang I have a warning (in vC ++ it works fine):

warning: the explicit specialization of "Helper" within the class is the Microsoft Extension [-Wmicrosoft]

#include <stdio.h>
#include <string>

enum class Car { BMW };

class C
{
    static void Method() { puts("inner");}
};

template<typename T>
class BaseClass
{
private:
    template<typename V> 
    struct Helper;

    template<>
    struct Helper<Car>
    {
        typedef C InnerType;
        static const char* Name() { return "Car"; }
    };

    typedef Helper<T> Info;
    typedef typename Info::InnerType InnerType;

private:
    T v;

protected:
    BaseClass()
    { }

public:
    T Value() const { return v; }
    std::string Name() const { return Info::Name(); }
    static void Print() { InnerType::Method(); }
};


class MyCar : public BaseClass<Car>
{
public:
    MyCar() : BaseClass() {}
};

int main()
{
    MyCar a;
    printf("%s\n", a.Name().c_str());
//  a.Print();
}

I tried to move the specialization of the Helper class out BaseClassfor compatibility with the standard:

template<> template<>
struct BaseClass<Car>::Helper<Car>
{
    typedef C InnerType;
    static const char* Name() { return "Car"; }
};

But now I have a compilation error:

error: implicit template creation undefined "BaseClass :: Helper"

If I delete the line: typedef typename Info::InnerType InnerType;(and the associated use in the function Print), then everything will be fine.

Can this error be fixed? I would like to keep the class Helperas private.

+4
source share
1 answer

You can do it as follows:

#include <stdio.h>
#include <string>

enum class Car { BMW };

class C
{
    static void Method() { puts("inner");}
};

template<typename T>
class BaseClass
{
private:
    template<typename V> 
    struct Helper;

    template<typename V>
    using Info = Helper<V>;

    template<typename V>
    using InnerType = typename Info<V>::InnerType;


private:
    T v;

protected:
    BaseClass()
    { }

public:
    T Value() const { return v; }
    std::string Name() const { return Info<T>::Name(); }
    static void Print() { InnerType<T>::Method(); }
};

template<> template<>
struct BaseClass<Car>::Helper<Car>
{
    typedef C InnerType;
    static const char* Name() { return "Car"; }
};


class MyCar : public BaseClass<Car>
{
public:
    MyCar() : BaseClass() {}
};

int main()
{
    MyCar a;
    printf("%s\n", a.Name().c_str());
    //a.Print();
}

(gcc 5.1/clang 3.6, -std = ++ 11)

"".

+2

All Articles