C++11如何创建模板函数,以最小的专业化将基元和std::vector转换为std::string



标题中描述了我卡住的地方。

但是,也许,我的全球问题还有另一个解决方案。

有一些模板基类,它做了一些工作,我需要打印结果以进行调试。


template<typename ResultType>
class Foo
{
public:
void PrintResult()
{
std::cout<<toString<ResultType>(result)<<std::endl;
}
virtual void Do(std::vector<void*> args)=0;
virtual ~Foo(){}
protected:
ResultType result;
};

我主要需要打印基元、std::string和基元、字符串或向量的std::vector。

我决定使用模板来实现这一点,但我不能专门化std::vector的模板。我不知道为什么它选择了错误的过载。


#include <iostream>
#include <sstream>
#include <vector>
template <typename T>
std::string toString(const T& obj)
{
return std::to_string(obj);
}
template <typename T>
std::string toString(const std::vector<T>& obj)
{
std::stringstream ss;
ss<<"[";
for(size_t i=0;i<obj.size();++i)
{
ss<<toString<T>(obj[i]);
if(i!=obj.size()-1)
ss<<", ";
}
ss<<"]";
return ss.str();
}
int main()
{
int intValue = 42;
std::vector<int> vectorOfInts;
std::vector<std::vector<double>> vectorOfVectorsOfDoubles;
std::cout<<toString<int>(intValue)<<std::endl;
std::cout<<toString<std::vector<int>>(vectorOfInts)<<std::endl;
std::cout<<toString<std::vector<std::vector<double>>>(vectorOfVectorsOfDoubles)<<std::endl;
return EXIT_SUCCESS;
}

错误:


./main.cpp:-1: In instantiation of ‘std::__cxx11::string toString(const T&) [with T = std::vector<int>; std::__cxx11::string = std::__cxx11::basic_string<char>]’:
./main.cpp:33: required from here
./main.cpp:8: error: no matching function for call to ‘to_string(const std::vector<int>&)’
return std::to_string(obj);
~~~~~~~~~~~~~~^~~~~
./main.cpp:-1: In instantiation of ‘std::__cxx11::string toString(const T&) [with T = std::vector<std::vector<double> >; std::__cxx11::string = std::__cxx11::basic_string<char>]’:
./main.cpp:34: required from here
./main.cpp:8: error: no matching function for call to ‘to_string(const std::vector<std::vector<double> >&)’
return std::to_string(obj);
~~~~~~~~~~~~~~^~~~~

编译器:

GCC 4.8(C++,x86 64位(

是否存在不使用所有基元和向量专门化模板函数的可能性?

我尝试从C++如何使用vector<T>?带有来自的c++11的修复程序https://ru.stackoverflow.com/questions/500735/stdenable-if-t

但我仍然会遇到错误:

#include <iostream>
#include <sstream>
#include <vector>
#include <type_traits>
template<bool C, class T = void>
using enable_if_t = typename std::enable_if<C, T>::type;
template<typename T>
struct is_vector
{
static constexpr bool value = false;
};
template<template<typename...> class C, typename U>
struct is_vector<C<U>>
{
static constexpr bool value =
std::is_same<C<U>,std::vector<U>>::value;
};
template <typename T>
enable_if_t<!is_vector<T>::value,std::string> toString(T& obj)
{
return std::to_string(obj);
}
template <typename T>
enable_if_t<is_vector<T>::value,std::string> toString(std::vector<T>& obj)
{
std::stringstream ss;
ss<<"[";
for(size_t i=0;i<obj.size();++i)
{
ss<<toString<T>(obj[i]);
if(i!=obj.size()-1)
ss<<", ";
}
ss<<"]";
return ss.str();
}
int main()
{
int intValue = 42;
std::vector<int> vectorOfInts;
std::vector<std::vector<double>> vectorOfVectorsOfDoubles;
std::cout<<toString<int>(intValue)<<std::endl;
std::cout<<toString<std::vector<int>>(vectorOfInts)<<std::endl;
std::cout<<toString<std::vector<std::vector<double>>>(vectorOfVectorsOfDoubles)<<std::endl;
return EXIT_SUCCESS;
}
./main.cpp:-1: In function ‘int main()’:
./main.cpp:50: error: no matching function for call to ‘toString<std::vector<int, std::allocator<int> > >(std::vector<int>&)’
std::cout<<toString<std::vector<int>>(vectorOfInts)<<std::endl;
^
./main.cpp:23: candidate: template<class T> enable_if_t<(! is_vector<T>::value), std::__cxx11::basic_string<char> > toString(T&)
enable_if_t<!is_vector<T>::value,std::string> toString(T& obj)
^~~~~~~~
./main.cpp:23: note:   template argument deduction/substitution failed:
./main.cpp:29: candidate: template<class T> enable_if_t<is_vector<T>::value, std::__cxx11::basic_string<char> > toString(std::vector<T>&)
enable_if_t<is_vector<T>::value,std::string> toString(std::vector<T>& obj)
^~~~~~~~
./main.cpp:29: note:   template argument deduction/substitution failed:
./main.cpp:50: note:   cannot convert ‘vectorOfInts’ (type ‘std::vector<int>’) to type ‘std::vector<std::vector<int>, std::allocator<std::vector<int> > >&’
std::cout<<toString<std::vector<int>>(vectorOfInts)<<std::endl;
^
./main.cpp:51: error: no matching function for call to ‘toString<std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > >(std::vector<std::vector<double> >&)’
std::cout<<toString<std::vector<std::vector<double>>>(vectorOfVectorsOfDoubles)<<std::endl;
             ^
./main.cpp:23: candidate: template<class T> enable_if_t<(! is_vector<T>::value), std::__cxx11::basic_string<char> > toString(T&)
enable_if_t<!is_vector<T>::value,std::string> toString(T& obj)
^~~~~~~~
./main.cpp:23: note:   template argument deduction/substitution failed:
./main.cpp:29: candidate: template<class T> enable_if_t<is_vector<T>::value, std::__cxx11::basic_string<char> > toString(std::vector<T>&)
enable_if_t<is_vector<T>::value,std::string> toString(std::vector<T>& obj)
^~~~~~~~
./main.cpp:29: note:   template argument deduction/substitution failed:
./main.cpp:51: note:   cannot convert ‘vectorOfVectorsOfDoubles’ (type ‘std::vector<std::vector<double> >’) to type ‘std::vector<std::vector<std::vector<double> >, std::allocator<std::vector<std::vector<double> > > >&’
std::cout<<toString<std::vector<std::vector<double>>>(vectorOfVectorsOfDoubles)<<std::endl;
             ^

您为函数调用显式选择了错误的模板实例化。如果您删除模板参数列表,并让编译器自动推导它们,它会起作用:

编译器资源管理器

#include <iostream>
#include <sstream>
#include <vector>
template <typename T>
std::string toString(const T& obj)
{
return std::to_string(obj); // <- no template param list
}
template <typename T>
std::string toString(const std::vector<T>& obj)
{
std::stringstream ss;
ss<<"[";
for(size_t i=0;i<obj.size();++i)
{
ss<<toString(obj[i]); // <- no template param list
if(i!=obj.size()-1)
ss<<", ";
}
ss<<"]";
return ss.str();
}
template<typename ResultType>
class Foo
{
public:
void PrintResult()
{
std::cout << toString(result)<<std::endl; // <- no template param list
}
virtual void Do(std::vector<void*> args)=0;
virtual ~Foo(){}
protected:
ResultType result;
};

int main()
{
int intValue = 42;
std::vector<int> vectorOfInts;
std::vector<std::vector<double>> vectorOfVectorsOfDoubles;
std::cout<<toString<int>(intValue)<<std::endl; // <- no template param list
std::cout<<toString(vectorOfInts)<<std::endl; // <- no template param list
std::cout<<toString(vectorOfVectorsOfDoubles)<<std::endl; // <- no template param list
return EXIT_SUCCESS;
}

最新更新