c++ - <未解析的重载函数类型> 带有成员函数指针和模板

标签 c++ templates c++11 pointer-to-member

抱歉我的问题有点复杂,这里是:

我有一个辅助结构,它包含任何非类型模板参数,声明如下:

template<typename T, T t>
struct Method {
    using Type = T;
    constexpr static T method = t;
};

我用它来存储一个成员函数指针,以便在编译时使用。

然后,我有一个名为 TestClass 的类,它有一个 Method 类型的 typedef,其中一个方法作为参数:

struct TestClass {
    void setFoo(int n) {}

    using MethodToCall = Method<decltype(&TestClass::setFoo), &TestClass::setFoo>;
};

此时一切正常,我可以这样调用它:

(tc.*TestClass::MethodToCall::method)(6); // compiles and runs

然后,问题来了:我有另一个成员函数再次将方法作为模板参数,我正试图从中获取一个指针。仅当我直接放置函数指针时它才有效。

struct Caller {
    template<typename T, T t>
    void callme(TestClass& test) {
        (test.*t)(6);
    }
};

template<typename F, typename T, typename... Args>
auto call(F f, T t, Args&&... args) -> decltype((t.*f)(std::forward<Args>(args)...)) {
    (t.*f)(std::forward<Args>(args)...);
}

int main() {
    Caller c;
    TestClass tc;

    (tc.*TestClass::MethodToCall::method)(6);

    //call(&Caller::callme<TestClass::MethodToCall::Type, TestClass::MethodToCall::method>, c, tc);
    call(&Caller::callme<decltype(&TestClass::setFoo), &TestClass::setFoo>, c, tc);

    return 0;
}

前三个调用是行不通的。为什么是这样?我该如何解决? 如果我取消注释第一个调用,编译会给我这个错误:

main.cpp: In function 'int main()':
main.cpp:40:96: error: no matching function for call to 'call(<unresolved overloaded function type>, Caller&, TestClass&)'
     call(&Caller::callme<TestClass::MethodToCall::Type, TestClass::MethodToCall::method>, c, tc);
                                                                                                ^
main.cpp:30:6: note: candidate: template<class F, class T, class ... Args> decltype (call::t.*call::f((forward<Args>)(call::args)...)) call(F, T, Args&& ...)
 auto call(F f, T t, Args&&... args) -> decltype((t.*f)(std::forward<Args>(args)...)) {
      ^
main.cpp:30:6: note:   template argument deduction/substitution failed:
main.cpp:40:96: note:   couldn't deduce template parameter 'F'
     call(&Caller::callme<TestClass::MethodToCall::Type, TestClass::MethodToCall::method>, c, tc);
                                                                                                ^

完整代码可以在这里找到:http://coliru.stacked-crooked.com/a/821c5b874b45ffb9

非常感谢!

最佳答案

我发现了错误。您不能将成员函数指针发送到存储在变量中的模板参数,即使是 constexpr 也是如此。

constexpr auto theMethod = &TestClass::setFoo;

// won't work
call(&Caller::callme<decltype(theMethod), theMethod>);

作为成员函数指针的模板参数,要求直接写成:

constexpr auto theMethod = &TestClass::setFoo;

// won't work
call(&Caller::callme<decltype(theMethod), &TestClass::setFoo>);

我的解决方案是传递 std::integral_constant 而不是直接传递函数指针。

using methodType = std::integral_constant<decltype(&TestClass::setFoo), &TestClass::setFoo>;

call(&Caller::callme<methodType>);

关于c++ - <未解析的重载函数类型> 带有成员函数指针和模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35420195/

相关文章:

c++ - 对象组合促进代码重用。 (T/F,为什么)

c++ - opengl 显示白框而不是纹理

c++ - 如何判断一个类型是否派生自模板类?

c++ - lambda 函数是否需要任何头文件?

c++ - 通过 std::unique_ptr 使用 std::map 访问运算符 [] 的正确语法

c++ - 为什么 g++ 4.9.0 默认有 std::isnan?

c++ - 在 g++ (mingw) 编译的应用程序中使用用 visual studio 编译的库

c++ - 使用 visual studio 2012 构建 OpenCV 2.49

templates - Golang索引模板包括

C++ 模板化容器扫描器