Error transferring std :: vector as a template template parameter - works in GCC, failure in MSVC

Following code

#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; } 

works great with GCC ( link ). However, it does not compile with MSVC 2013 (12.0) with the error: 'std::vector' : class has no constructors (can be tested here , select compiler version 12.0). What is the problem here, and how can I fix it?

+8
c ++ templates
source share
1 answer

The code:

 #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; } 

They worked. The problem is that there is a variational number of template parameters ...

Editorial: Actually not with a variable number of template parameters, since I can even compile it with

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

therefore, the MSVC compiler must explicitly specify each type of template.

+4
source share

All Articles