小编Min*_* Lê的帖子

如何为C++模板完成功能解析?

困扰我的代码发布在这里:

namespace ns1 {
    struct myStruct1 {};
    struct myStruct2 {};
}

namespace ns2 {
    template <typename T>
    constexpr int foo(T& x) {
        return 1;
    }

    // If the two functions below are switched, it returns 2 correctly
    template <typename T>
    constexpr int fooCaller(T& x) {
        return foo(x);
    }

    constexpr int foo(ns1::myStruct2& x) {
        return 2;
    }
}

// If the below is uncommented, it also returns 2 correctly
/*
namespace ns1 {
    constexpr int foo(myStruct2& x) {
        return 2;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading function overload-resolution

3
推荐指数
1
解决办法
84
查看次数