C++ 如何在手动编码时以简单的方式检测模板函数的适当返回类型

C++ 如何在手动编码时以简单的方式检测模板函数的适当返回类型,c++,visual-studio,visual-c++,boost,C++,Visual Studio,Visual C++,Boost,我的旧项目仍然使用VS2008,它不能支持c++11。而它使用boost_1_43_0。我编写了以下代码,然后用一个临时变量和一个函数调用替换了重复的函数调用,但我无法轻松确定函数的返回类型(c++11的noauto) 我如何能得到一个简单的方法来定义这样一个变量 #include <string> #include <set> #include <boost/algorithm/string/trim.hpp> // #include <boost/a

我的旧项目仍然使用VS2008,它不能支持c++11。而它使用boost_1_43_0。我编写了以下代码,然后用一个临时变量和一个函数调用替换了重复的函数调用,但我无法轻松确定函数的返回类型(c++11的no
auto

我如何能得到一个简单的方法来定义这样一个变量

#include <string>
#include <set>
#include <boost/algorithm/string/trim.hpp>
// #include <boost/algorithm/string.hpp>
// #include <boost/algorithm/string/classification.hpp>
// #include <boost/algorithm/string/predicate.hpp>


std::wstring wsExtraHeaders(L"X-Forwarded-For, X-Requested-With, X-UA-Compatible,   X-Powered-By, ");
std::set<std::wstring> extraHeaderKeys;

// Old code

boost::algorithm::trim_if(wsExtraHeaders, boost::is_any_of(L",\t "));
boost::algorithm::split(extraHeaderKeys, wsExtraHeaders, boost::is_any_of(L",\t "), boost::token_compress_on);

// New code

boost::algorithm::detail::is_any_ofF<wchar_t> delemiterPred = boost::is_any_of(L",\t "); // here, determine the return type
boost::algorithm::trim_if(wsExtraHeaders, delemiterPred);
boost::algorithm::split(extraHeaderKeys, wsExtraHeaders, delemiterPred, boost::token_compress_on);

// C++11, but `auto` cannot be used in VS2008
// auto delemiterPred = boost::is_any_of(L",\t ");
——更新后的

VS2008和VS2013 IDE工具提示

vs2013 IDE可以检测@Angew选项2


由于您不能使用
自动
(这当然是最好的选择),我可以为您提供两个选项,每个选项都有自己的缺点:

  • 将确切的类型抽象为更通用但可表达的类型。对于这样的谓词,正确的类型应该是:

    boost::function<bool(wchar_t)> delemiterPred = boost::is_any_of(L",\t ");
    
    boost::function deletiterpred=boost::是(L“,\t”)中的任意一个;
    
    缺点是您现在可能要为
    boost::function
    中的类型擦除支付(部分)运行时性能。当然,问题是这一点代码是否是应用程序的瓶颈

  • 深入Boost的实现,复制并粘贴
    Boost::is_any_of
    定义中的确切返回类型。缺点是,当您升级到可能具有不同返回类型的不同Boost版本时,这可能会中断


  • 这取决于你决定哪些缺点更适合你。除非真实数据上的性能分析告诉我其他情况,否则出于可维护性的原因,我会选择选项1。

    “是否最好……”==基于意见的问题。:)我认为它们是一致的好观点!我似乎没有发现选项1的缺点:)
    boost::function<bool(wchar_t)> delemiterPred = boost::is_any_of(L",\t ");