C ++ 11-std :: declval< T>()方法的名称 [英] C++11 - std::declval<T>() name of method

查看:149
本文介绍了C ++ 11-std :: declval< T>()方法的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法,如何在 std :: declval< T>()

到目前为止,我有这个:

So far, I have this:

template<typename T, typename ... Args>
struct MethodInfo
{
    using type = decltype(std::declval<T>().foo(std::declval<Args>() ...)) (T::*)(Args ...);
};

但我想 foo 作为模板参数。

推荐答案

这不是你想要的,但我认为这可能有点适合你的需要:

It's not exactly what you asked but I think this may somewhat fit your need:

#include <type_traits>
#include <tuple>
#include <iostream>

template<typename T, typename... Args>
struct MethodInfo {
    template<typename Ret>
    static auto get(Ret(T::*)(Args...)) -> Ret(T::*)(Args...);
};

struct Foo {
    int foo(int);
    int foo(int, int);
};

int main() {  
    static_assert(std::is_same<
                        int(Foo::*)(int), 
                        decltype(MethodInfo<Foo, int>::get(&Foo::foo))
                          >::value, "");
}

演示

因为,函数名是一个非类型模板参数,我认为这是唯一的解决方案,到现在为止。

Because, the function name is a non-type template parameter, I think this is the only solution, up to now.

这篇关于C ++ 11-std :: declval&lt; T&gt;()方法的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆