2015-03-17 11 views
8

następujący kodBłąd przechodzącej std :: vector jako parametr szablonu szablonu - pracuje w GCC, nie w MSVC

#include <iostream> 
#include <vector> 
#include <algorithm> 
#include <iterator> 
#include <deque> 
#include <functional> 

#define BEGIN_TO_END(container) container.begin(), container.end() 

template <template<typename...> class OutputContainerType, class InContainer> 
OutputContainerType<typename InContainer::value_type> convertContainer(const InContainer& in) 
{ 
    OutputContainerType<typename InContainer::value_type> result; 
    std::transform(BEGIN_TO_END(in), std::back_inserter(result), [](typename InContainer::value_type value) {return value;}); 
    return result; 
} 

int main() { 
    std::deque<int> d {1, 2, 3}; 
    const auto v = convertContainer<std::vector>(d); 
    std::cout << v.size() << std::endl; 
} 

działa poprawnie z GCC (link). Jednak nie kompiluje się z MSVC 2013 (12.0) z błędem: 'std::vector' : class has no constructors (można przetestować here, wybierz wersję kompilatora 12.0). Jaki jest problem i jak mogę to naprawić?

+2

Bez problemu. Tylko że MSVC, jak zwykle, ma błąd w tym przypadku. – Nawaz

+0

@Nawaz: Trudno uwierzyć, że coś tak podstawowego, jak parametr szablonu szablonu, nie byłby objęty testami kompilatora. Czy jest to spowodowane określeniem 'std :: vector'? –

+0

Btw. BEGIN_TO_END można rozwiązać za pomocą Boost.Range: http://www.boost.org/doc/libs/1_57_0/libs/range/doc/html/range/reference/algorithms/mutating/transform.html. Używanie makra nie jest dobrym stylem. – sfrehse

Odpowiedz

4

Kod:

#include <iostream> 
#include <vector> 
#include <algorithm> 
#include <iterator> 
#include <deque> 
#include <functional> 

#define BEGIN_TO_END(container) container.begin(), container.end() 

template <template<typename T, typename T2> class OutputContainerType, class InContainer> 
OutputContainerType<typename InContainer::value_type, std::allocator<typename InContainer::value_type>> convertContainer(const InContainer& in) 
{ 
    OutputContainerType<typename InContainer::value_type, std::allocator<typename InContainer::value_type>> result; 
    std::transform(BEGIN_TO_END(in), std::back_inserter(result), [](typename InContainer::value_type value) {return value;}); 
    return result; 
} 

int main() { 
    std::deque<int> d {1, 2, 3}; 
    const auto v = convertContainer<std::vector>(d); 
    std::cout << v.size() << std::endl; 
} 

działało. Problem jest wtedy z numerem zmiennej liczbie argumentów parametrów szablonu tutaj ...

edycja: Właściwie nie z numerem zmiennej liczbie argumentów parametrów szablonu jak mogę nawet skompilować go z

template <template<typename...> class OutputContainerType, class InContainer> 

tak więc MSVC kompilator musi jawnie podać każdy typ szablonu.

+0

Dzięki! Wygląda na to, że problem dotyczy domyślnego parametru szablonu. –

Powiązane problemy