C ++ template constructor will not compile

Why can't I instantiate an object of type Foo using the above constructor?

I have a Bar class that uses an internal typedef (as a workaround for the "typedefs template") and intends to use it in the constructor, as shown below (CASE 1). However, it seems I'm not going to compile it. Is this legal C ++? CASE 2 seems to indicate that the problem is with typedef in Bar.

How can I define a constructor that will accept std :: vectors of objects with type in Bar?

#include <vector>
#include <iostream>
#include <utility>

template <typename T>
struct Bar
{
    typedef std::pair<T, T> type; // or anything else that uses T
};

struct Foo
{
    Foo() {}

    // CASE 1: doesn't compile
    template <typename T> explicit Foo( const std::vector<typename Bar<T>::type>& data )
    {
        std::cout << "Hello\n";
    }

    //// CASE 2: compiles, but it not what I want
    //template <typename T> explicit Foo( const std::vector<Bar<T> >& data )
    //{
    //  std::cout << "Hello\n";
    //}
};

int main()
{
    std::vector<Bar<int>::type> v; // for CASE 1
    //std::vector<Bar<int> > v; // for CASE 2

    Foo f( v );
    return 0;
}
+5
source share
4 answers

14.8.2.1 ++, , :

- , - .

, . 14.8.2.4:

:

  • , .

  • , , - , -.

Bar<T>::type, Bar<T> - , , , ... (.. Foo f<int>(v)).

, , , , , , : , Bar :

template<typename T>
struct Bar
{
    typedef std::pair<T,T> type;
};

template<>
struct Bar<char>
{
    typedef std::pair<int,int> type;
};

Foo std::vector<std::pair<int,int> >: int char? , , Bar , typedef (, , , , , : -)!)

+9

:

template <typename T> 
explicit 
Foo( const std::vector<typename Bar<T>::type>& data )

T . ( , " ", .)

.

template <typename B> 
explicit 
Foo( const std::vector<B>& data )

, B typename Bar<T>::type.

+4

, , "".

, ( , , ):

#include <utility>
#include <vector>

template <class T>
struct BarType;

template <class T>
struct BarType<std::pair<T, T> >
{
    typedef T type;
};

template <class T>
struct Bar {};

struct Foo
{
    template <class T>
    Foo(const std::vector<T>& )
    {
        Bar<typename BarType<T>::type> bar;
        //...
    }
};

int main()
{
    Foo(std::vector<std::pair<int, int> >());
}
0
source

- ?

#include <vector>
#include <iostream>
#include <utility>
#include <boost/static_assert.hpp>

template <typename T>
struct Bar
{
    typedef std::pair<T, T> type; // or anything else that uses T
    enum {val = 42};
};

template <typename T>
struct Traits
{
    enum {allowed = false};
};

template <typename T>
struct Traits<std::pair<T, T> >
{
    enum {allowed = true};
    typedef Bar<T> BarType;
};

struct Foo
{
    Foo() {}

    template <typename T> explicit Foo( const std::vector<T>& data )
    {
        BOOST_STATIC_ASSERT(Traits<T>::allowed);
        typedef typename Traits<T>::BarType BarType;
        std::cout << BarType::val << std::endl;
    }
};

int main()
{
    std::vector<Bar<int>::type> v;
    std::vector<float> v2;

    Foo f( v );
//    Foo f2( v2 ); // Compile error
    return 0;
}

GCC 4.4.1. Traits vector::value_type, .

0
source

All Articles