C ++ 0x inherited constructor in templates

Here is the foo class:

template <typename T>
struct foo
{
    foo()
    {
        t = nullptr;
    }

    foo(T* p, bool flag)
    {
        t = p;
    }
private:
    T* t;
};

Here is the class panel:

template <typename T>
struct bar: public foo<T>
{
    using foo<T>::foo<T>;
};

Is this the correct syntax for constructor inheritance? If I use "using foo :: foo;" then the Visual C ++ 2010 compiler dies. So basically, how do you inherit constructors from template classes in VC ++ 2010?

+5
source share
4 answers
template <typename T>
struct bar: public foo<T>
{
    using foo<T>::foo<T>;
};

, template foo<T>;, , foo ( foo<T>, , T ). ::template . bar: (T - ) , :

template<typename T>
foo();

, template-id (, foo<T>) ( ), , ::template ( ), - .

, , : ( ...::...), , :

  • ( foo<T>), ( foo<T>::foo TTP<T>::TTP TTP ).
  • (, foo::foo T::T, T ).

. , , ++ 03. , ++ 03: , :

  • foo::foo . T::T ( T foo) , foo , T.

, ,

using foo<T>::foo;
using bar::foo::foo; // valid too

: foo - , foo<T> bar. bar::foo, foo, , `foo.

, , , ( ): foo<T>::foo , <T>, , .

+9

, , rvalue, _trait, :

#include <type_traits>
#include <utility>
#include <ostream>

enum Color {Red, Blue};

#define USING(Derived, Base)                                 \
    template<typename ...Args,                               \
             typename = typename std::enable_if              \
             <                                               \
                std::is_constructible<Base, Args...>::value  \
             >::type>                                        \
    Derived(Args &&...args)                                  \
        : Base(std::forward<Args>(args)...) { }              \


template<typename Mixin>
class add_color
: public Mixin
{
    Color color;

public:
    USING(add_color, Mixin);

    friend std::ostream& operator<<(std::ostream& os, const add_color& x)
    {
        switch (x.color)
        {
        case Red:
            os << "Red";
            break;
        case Blue:
            os << "Blue";
            break;
        }
        os << ' ' << x.first << ' ' << x.second;
        return os;
    }
};

#include <string>
#include <iostream>

int main()
{
    add_color<std::pair<std::string, int>> x1("five", 5);
    std::cout << "x1 = " << x1 << '\n';
    add_color<std::pair<std::string, int>> x3;
    std::cout << "x3 = " << x3 << '\n';
    add_color<std::pair<std::string, int>> x4 = x1;
    std::cout << "x4 = " << x4 << '\n';
    std::pair<std::string, int> p;
    add_color<std::pair<std::string, int>> x5 = p;
    std::cout << "x5 = " << x5 << '\n';
}

is_constructible, , " " .

+6

;

template <typename T>
struct bar: public foo<T>
{
    using foo<T>::foo;
};

. , g++ - 4.4.1, , .

+3

, ++ 0x. , , ++ 0x. , , V++ 2010 .

++ 0x . , . , ++ 0x, .

I believe the latest version of GCC supports constructor inheritance, so if you should try it now, you can use this. Of course, C ++ 0x support is experimental and may change as errors are detected, etc.

+2
source

All Articles