Using member declaration with enable_if?

I need conditional use of a member ad.

template <bool> struct B;
template <> struct B<true> { void foo(); };
template <> struct B<false> { };

template <typename T>
struct A : public B<is_default_constructible<T>::value> {
    using B<is_default_constructible<T>::value>::foo();

    void foo(int) {}
};

This clearly does not work, because it is B<bool>::foonot defined in half the cases. How can i achieve this? To have B<>::foo() visible in the area A<T>next to foo (int)?

thanks for the help

+4
source share
2 answers

This is my decision. I am sure that this will not be the best, but he will do his job.

struct A {
    void foo(int) {}
};

struct A should contain the methods that you want to define in both cases.

template <bool> struct B;
template <> struct B<false> : A {};
template <> struct B<true> : A { 
    using A::foo;
    void foo() {} 

};

In the case B<false>is determined only void foo(int). In the case B<true>defined as void foo(int)and void foo().

template <typename T>
struct C : public B<is_default_constructible<T>::value> {};

, B<is_default_constructible<T>::value>::foo() .

class D { D() = delete; };

int main()
{
    C<int> c1;
    c1.foo(1234);
    c1.foo();
    // both methods are defined for C<int>

    C<D> c2;
    c2.foo(1234);
    // c2.foo(); // undefined method

    return 0;
}
0

.

enable_if . struct A.

#include <type_traits>

template <bool> struct B;
template <> struct B<true> { void foo(); };
template <> struct B<false> { };

template <typename T, bool default_constructible = std::is_default_constructible<T>::value>
struct A : public B<default_constructible> {
    using B<default_constructible>::foo;

    void foo(int) {}
};

template<typename T>
struct A<T, false> : public B<false> {
    void foo(int) {}
};

foo (int)

foo(int) , :

#include <type_traits>

template <bool> struct B;
template <> struct B<true> { void foo(); };
template <> struct B<false> { };

template<typename T>
struct C {
  void foo(int) {}
};

template <typename T, bool default_constructible = std::is_default_constructible<T>::value>
struct A : public B<default_constructible>, public C<T> {
    using B<default_constructible>::foo;
    using C<T>::foo;
};

template<typename T>
struct A<T, false> : public B<false>, public C<T> {
    using C<T>::foo;
};

bool

, bool struct A, foo . struct A, .

#include <type_traits>

template <bool> struct B;
template <> struct B<true> { void foo(); };
template <> struct B<false> { };

template<typename T>
struct C {
  void foo(int) {}
};

template <typename T, bool default_constructible = std::is_default_constructible<T>::value>
struct base_A : public B<default_constructible>, public C<T> {
    using B<default_constructible>::foo;
    using C<T>::foo;
};

template<typename T>
struct base_A<T, false> : public B<false>, public C<T> {
    using C<T>::foo;
};

template <typename T>
struct A : public base_A<T> {
    // Other members.
};
0

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


All Articles