将 std::vector 作为模板模板参数传递时出错 - 在 GCC 中有效,在 MSVC 中失败
Error passing std::vector as a template template parameter - works in GCC, fails in 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;
}
适用于 GCC(link). However, it doesn't compile with MSVC 2013 (12.0) with the error: 'std::vector' : class has no constructors
(can be tested here、select 12.0 编译器版本)。这里有什么问题,我该如何解决?
代码:
#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;
}
成功了。问题是这里有可变数量的模板参数...
已编辑:
实际上不使用可变数量的模板参数,因为我什至可以用
编译它
template <template<typename...> class OutputContainerType, class InContainer>
因此 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;
}
适用于 GCC(link). However, it doesn't compile with MSVC 2013 (12.0) with the error: 'std::vector' : class has no constructors
(can be tested here、select 12.0 编译器版本)。这里有什么问题,我该如何解决?
代码:
#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;
}
成功了。问题是这里有可变数量的模板参数...
已编辑: 实际上不使用可变数量的模板参数,因为我什至可以用
编译它template <template<typename...> class OutputContainerType, class InContainer>
因此 MSVC 编译器需要显式指定模板的每种类型。