LINUX.ORG.RU

История изменений

Исправление rymis, (текущая версия) :

Мое предложение: (Я не видел предыдущих аналогичных ответов, но смысл один)

#include <functional>

class PublicFooBar {
public:
    void bar() {
    }
};

class PrivateFooBar { // or protected if you want to use this method inside the class...
private:
    void bar() {
    }
};

template <typename T>
class Foo: public std::conditional<std::is_integral<T>::value, PublicFooBar, PrivateFooBar>::type {
    using Parent = typename std::conditional<std::is_integral<T>::value, PublicFooBar, PrivateFooBar>::type;
public:
    ~Foo() { }

    using Parent::bar;
};

int main() {
    Foo<int> foo1;

#ifdef FAIL
    Foo<char*> foo2;
#endif

    return 0;
}
$ g++ main.cpp 
$ g++ -DFAIL main.cpp 
main.cpp: In instantiation of ‘class Foo<char*>’:
main.cpp:28:13:   required from here
main.cpp:16:7: error: ‘void PrivateFooBar::bar()’ is private within this context
 class Foo: public std::conditional<std::is_integral<T>::value, PublicFooBar, PrivateFooBar>::type {
       ^~~
main.cpp:11:7: note: declared private here
  void bar() {
       ^~~

Исходная версия rymis, :

Мое предложение:

#include <functional>

class PublicFooBar {
public:
    void bar() {
    }
};

class PrivateFooBar { // or protected if you want to use this method inside the class...
private:
    void bar() {
    }
};

template <typename T>
class Foo: public std::conditional<std::is_integral<T>::value, PublicFooBar, PrivateFooBar>::type {
    using Parent = typename std::conditional<std::is_integral<T>::value, PublicFooBar, PrivateFooBar>::type;
public:
    ~Foo() { }

    using Parent::bar;
};

int main() {
    Foo<int> foo1;

#ifdef FAIL
    Foo<char*> foo2;
#endif

    return 0;
}
$ g++ main.cpp 
$ g++ -DFAIL main.cpp 
main.cpp: In instantiation of ‘class Foo<char*>’:
main.cpp:28:13:   required from here
main.cpp:16:7: error: ‘void PrivateFooBar::bar()’ is private within this context
 class Foo: public std::conditional<std::is_integral<T>::value, PublicFooBar, PrivateFooBar>::type {
       ^~~
main.cpp:11:7: note: declared private here
  void bar() {
       ^~~