C++ C+的兼容性+;11针对Visual Studio 2008的代码

C++ C+的兼容性+;11针对Visual Studio 2008的代码,c++,c++11,visual-studio-2008,backwards-compatibility,C++,C++11,Visual Studio 2008,Backwards Compatibility,我正在就如何支持C++11之前版本的编译器进行内部辩论—VisualStudio2008。这是为Windows 2000机箱构建所必需的(不要问!) 我使用了一些C++11特性,例如std::to_string、std::mutex等等。目前,我已经能够通过使用boost和tr1来编译底层项目,比如: #if _MSC_VER == 1500 # include <boost/thread/lock_guard.hpp> using boost::lock

我正在就如何支持C++11之前版本的编译器进行内部辩论—VisualStudio2008。这是为Windows 2000机箱构建所必需的(不要问!)

我使用了一些C++11特性,例如std::to_string、std::mutex等等。目前,我已经能够通过使用
boost
tr1
来编译底层项目,比如:

#if _MSC_VER == 1500    
#   include <boost/thread/lock_guard.hpp>    
    using boost::lock_guard;
#   include <boost/thread/mutex.hpp>
    using boost::mutex;
#   include <memory>
    using std::tr1::shared_ptr;
#else
#   include <mutex>
    using std::lock_guard;
    using std::mutex;

#   include <memory>
    using std::shared_ptr;
    using std::unique_ptr;
#endif
#如果_MSC_VER==1500
#包括
使用boost::lock_-guard;
#包括
使用boost::mutex;
#包括
使用std::tr1::shared\u ptr;
#否则
#包括
使用std::锁紧装置;
使用std::mutex;
#包括
使用std::shared_ptr;
使用std::unique\u ptr;
#恩迪夫
例如,将源代码更改为
shared_ptr func_name()
,而不是
std::shared_ptr func_name()
。远非十全十美,毫无疑问,很快就会有一些微妙的内部问题需要解决

我现在正试图找出
std::to_string
,它不能如此轻松地适应boost版本(在其他类中可能还有更多我还没有遇到的问题)

据我所知,我有两种选择:

1) 咬紧牙关,预处理器分离所有向后兼容的代码。对于99%的时间都不会被使用的东西,这将使代码变得丑陋和大量,但实现起来会快得多

2) 从std::string派生,并根据需要添加缺少的功能。对其他类/函数重复上述步骤。这将需要对不熟悉项目的人“使用未知类”,以及额外的时间编码来填补这些可能存在bug的空白,但这是最干净的解决方案。它也可以用于将来的兼容性修复

交换每种类型不是一项大任务,但我希望尽可能保留原始的实现类型,特别是因为代码也需要在Linux和BSD上运行。我没有做任何疯狂的事情,这也很难支持(没有元组、lambda或其他在这种情况下会有问题的东西)


以前是否有人做过类似的事情并找到了一个好的解决方案?

由于我没有足够的声誉来评论,我将尝试给出一个答案

to_字符串是一个独立的函数,因此不需要派生。根据速度/格式对您的重要性,您可以使用boost::lexical_cast或std::stringstream实现my::to_字符串模板函数。如果速度真的很重要,您应该投入更多的时间,手动创建使用std::sprintf的重载

以下是第一种方式的示例:

#include <boost/lexical_cast.hpp>
#include <string>
#include <iostream>

namespace my {
    template<typename T> std::string to_string(T val){
        return boost::lexical_cast<std::string>(val);
    }
}

int main(){
    std::string foo = my::to_string(42.23);
    std::cout << foo << std::endl;
    return 0;
}
#包括
#包括
#包括
名称空间我的{
模板std::字符串到_字符串(T val){
返回boost::词法转换(val);
}
}
int main(){
std::string foo=my::to_string(42.23);

std::无法在您自己的实现背后隐藏这两种语言标准之间的差异,并将丑陋的东西排除在代码的其余部分之外。这适用于您上面描述的内容,但不适用于
auto
、基于范围的for循环等功能。是的,我已经转换了一些简单的5行代码对于7/8行迭代器Monstrosite循环!如果没有更好的方法,我想我会总结大部分内容-从长远来看更好。标记为答案,因为它确实是最理想的解决方案,具有所提供的限制。但是,我将从visual studio支持中获得Windows 2000,并需要使用Another如果有人还想要它,我为你能放弃VS2008感到高兴!