gpt4 book ai didi

c++ - 重载 T 和 vector

转载 作者:行者123 更新时间:2023-11-30 00:58:38 25 4
gpt4 key购买 nike

我有一个静态方法 ToString(T)和重载 ToString(vector<T>) .这适用于正常使用,但不适用于 boost bind。请参阅我的示例代码:

#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <sstream>
#include <vector>

class MetaData
{
public:
template<typename T>
static std::string ToString(T inValue)
{
return boost::lexical_cast<std::string>(inValue);
}

template<typename T>
static std::string ToString(const std::vector<T> & inValues)
{
std::stringstream ss;
for (typename std::vector<T>::size_type idx = 0; idx < inValues.size(); ++idx)
{
if (idx != 0)
{
ss << ", ";
}
ss << inValues[idx];
}
return ss.str();
}

int getInt() { return 42; }

std::vector<int> getIntVector()
{
std::vector<int> result;
result.push_back(1);
result.push_back(2);
result.push_back(3);
return result;
}

void test();
};

void MetaData::test()
{
boost::function< std::string() > foo;

// Ok
foo = boost::bind(&ToString<int>, boost::bind(&MetaData::getInt, this));
std::cout << foo() << std::endl;

// Compiler errors
foo = boost::bind(&ToString<int>, boost::bind(&MetaData::getIntVector, this));
std::cout << foo() << std::endl;

// Ok
std::cout << ToString<int>(getIntVector()) << std::endl;
}

int main()
{
MetaData metaData;
metaData.test();
}

有人知道我做错了什么吗?

编辑
这些是我得到的编译器错误:

/usr/include/boost/bind/bind.hpp: In member function ‘R boost::_bi::list1<A1>::operator()(boost::_bi::type<R>, F&, A&, long int) [with R = std::basic_string<char>, F = std::basic_string<char> (*)(int), A = boost::_bi::list0, A1 = boost::_bi::bind_t<std::vector<int>, boost::_mfi::mf0<std::vector<int>, MetaData>, boost::_bi::list1<boost::_bi::value<MetaData*> > >]’:
/usr/include/boost/bind/bind_template.hpp:20:59: instantiated from ‘boost::_bi::bind_t<R, F, L>::result_type boost::_bi::bind_t<R, F, L>::operator()() [with R = std::basic_string<char>, F = std::basic_string<char> (*)(int), L = boost::_bi::list1<boost::_bi::bind_t<std::vector<int>, boost::_mfi::mf0<std::vector<int>, MetaData>, boost::_bi::list1<boost::_bi::value<MetaData*> > > >, boost::_bi::bind_t<R, F, L>::result_type = std::basic_string<char>]’
/usr/include/boost/function/function_template.hpp:132:42: instantiated from ‘static R boost::detail::function::function_obj_invoker0<FunctionObj, R>::invoke(boost::detail::function::function_buffer&) [with FunctionObj = boost::_bi::bind_t<std::basic_string<char>, std::basic_string<char> (*)(int), boost::_bi::list1<boost::_bi::bind_t<std::vector<int>, boost::_mfi::mf0<std::vector<int>, MetaData>, boost::_bi::list1<boost::_bi::value<MetaData*> > > > >, R = std::basic_string<char>]’
/usr/include/boost/function/function_template.hpp:913:60: instantiated from ‘void boost::function0<R>::assign_to(Functor) [with Functor = boost::_bi::bind_t<std::basic_string<char>, std::basic_string<char> (*)(int), boost::_bi::list1<boost::_bi::bind_t<std::vector<int>, boost::_mfi::mf0<std::vector<int>, MetaData>, boost::_bi::list1<boost::_bi::value<MetaData*> > > > >, R = std::basic_string<char>]’
/usr/include/boost/function/function_template.hpp:722:7: instantiated from ‘boost::function0<R>::function0(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = boost::_bi::bind_t<std::basic_string<char>, std::basic_string<char> (*)(int), boost::_bi::list1<boost::_bi::bind_t<std::vector<int>, boost::_mfi::mf0<std::vector<int>, MetaData>, boost::_bi::list1<boost::_bi::value<MetaData*> > > > >, R = std::basic_string<char>, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]’
/usr/include/boost/function/function_template.hpp:1064:16: instantiated from ‘boost::function<R()>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = boost::_bi::bind_t<std::basic_string<char>, std::basic_string<char> (*)(int), boost::_bi::list1<boost::_bi::bind_t<std::vector<int>, boost::_mfi::mf0<std::vector<int>, MetaData>, boost::_bi::list1<boost::_bi::value<MetaData*> > > > >, R = std::basic_string<char>, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]’
/usr/include/boost/function/function_template.hpp:1105:5: instantiated from ‘typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, boost::function<R()>&>::type boost::function<R()>::operator=(Functor) [with Functor = boost::_bi::bind_t<std::basic_string<char>, std::basic_string<char> (*)(int), boost::_bi::list1<boost::_bi::bind_t<std::vector<int>, boost::_mfi::mf0<std::vector<int>, MetaData>, boost::_bi::list1<boost::_bi::value<MetaData*> > > > >, R = std::basic_string<char>, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, boost::function<R()>&>::type = boost::function<std::basic_string<char>()>&]’
main.cpp:55:81: instantiated from here
/usr/include/boost/bind/bind.hpp:243:60: error: cannot convert ‘boost::_bi::result_traits<std::vector<int>, boost::_mfi::mf0<std::vector<int>, MetaData> >::type’ to ‘int’ in argument passing
make: *** [all] Error 1

最佳答案

ToString 转换为仿函数并为您的 vector 重载operator()。然后你可以通过 bind(ToString(), ....) 将它传递给绑定(bind),一切都会很好(如果你传递仿函数,绑定(bind)可以在内部解析模板,但如果你只是取一个函数指针)。

像这样:

struct ToString
{
template<typename T>
std::string operator()(T inValue) const
{
return boost::lexical_cast<std::string>(inValue);
}

template<typename T>
std::string operator()(const std::vector<T> & inValues) const
{
std::stringstream ss;
for (typename std::vector<T>::size_type idx = 0; idx < inValues.size(); ++idx)
{
if (idx != 0)
{
ss << ", ";
}
ss << inValues[idx];
}
return ss.str();
}
};

关于c++ - 重载 T 和 vector<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5913269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com