C++如何为标准库添加额外的库或函数

我们做C++项目时有时会因为跨项目、跨平台而不得不对C++自带的标准库添加一些额外的“补丁”。比如,对于Android平台的NDK所采用的llvm-clang编译工具链,当前仍然缺省诸如 <jthread><ranges> 等C++20起引入的标准库。再比如,MSVC编译器则是直接在C++17标准起,将一些老旧的类库给移除掉。为了对现有项目改动尽量少,我们可能往往会采取直接把这些缺失的类库给添加到当前C++的标准库之中,那我们该如何优雅地去解决这类问题呢?

笔者这里将直接在C++标准库的namespace——std 中插入缺失的类库或函数。我们来看以下例子:

#include <cstdio>

// 该名空间及其内部的实现可能是通过第三方库的头文件引入的
namespace myranges
{
    void foo()
    {
        puts("This is myranges::foo() function!");
    }
}

// 为标准库std名空间新增成员
namespace std
{
    // 对std名空间直接注入上面的 myranges 名空间
    namespace myranges = ::myranges;

    // 以下两个结构体在MSVC中从C++17标准起即被移除
#if defined(_MSC_VER) && __cplusplus >= 201703L

    template <typename ArgumentType, typename ResultType>
    struct unary_function
    {
        using argument_type = ArgumentType;
        using result_type = ResultType;
    };

    template <typename Arg1, typename Arg2, typename Result>
    struct binary_function
    {
        using first_argument_type = Arg1 ;
        using second_argument_type = Arg2 ;
        using result_type = Result ;
    };

#endif // _MSC_VER) && __cplusplus >= 201703L
}

// 下面我们可以来做些测试:
int main(int argc, const char* argv[])
{
    std::myranges::foo();

    struct my_inc : std::unary_function<int, int>
    {
        auto operator() (int i) -> int { return i + 1; }
    };

    struct my_is_less : std::binary_function<int, int, bool>
    {
        auto operator() (int a, int b) -> bool { return a < b; }
    };

    auto const a = my_inc{} (5);
    auto const res = my_is_less{} (a, 10);

    printf("a = %d, is less? %s!\n", a, res ? "YES" : "NO");
}

上述代码运行之后得到以下输出:

This is myranges::foo() function!

a = 6, is less? YES!

这里我们先用一个名空间 myranges 来表示缺失的某个类库,它可能是一个已经实现好的第三方类库,比如已经被包含在某个头文件里了。这里为了方便演示,直接写在了全局作用域。因此,我们在 namespace std 中也声明了一个叫 myranges 的名空间,然后直接对外部的 myranges 进行引用。

随后在下面,我们紧接着补上了MSVC在C++17开始被移除掉的两个类库—— unary_functionbinary_function。而且我们这里也利用了 __cplusplus 这个宏的值来判定当前所使用的C++标准是否大于等于2017版本。这里各位需要注意的是:在Visual Studio中,默认状态下,__cplusplus 这个宏将始终返回 199711L 这个值。因此我们必须要设置额外的编译选项使得该宏值能正常工作,请见下图所示。

我们在当前项目的属性配置页中,对“命令行”编译选项中的“其他选项”一栏添加 /Zc:__cplusplus。这样我们整个程序就能跑通了。

原文链接:,转发请注明来源!