Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 推断模板专门化中的依赖类型_C++_C++11_Templates_Lambda_Type Deduction - Fatal编程技术网

C++ 推断模板专门化中的依赖类型

C++ 推断模板专门化中的依赖类型,c++,c++11,templates,lambda,type-deduction,C++,C++11,Templates,Lambda,Type Deduction,我试图使用function_traits类来推断lambda的operator()成员函数指针,该指针使用常见的已知语法(class::*pointer)(Args list)。 但问题在于专门化中的typedefs/使用。看起来编译器没有使用my traits的类模板专门化。和stucks的一般情况 编译说 “函数\特性”中没有名为“结果\类型”的类型 #include <functional> #include <iostream> using namespace

我试图使用function_traits类来推断lambda的operator()成员函数指针,该指针使用常见的已知语法(class::*pointer)(Args list)。 但问题在于专门化中的typedefs/使用。看起来编译器没有使用my traits的类模板专门化。和stucks的一般情况 编译说

“函数\特性”中没有名为“结果\类型”的类型

#include <functional>
#include <iostream>

using namespace std;

template <typename T>
struct function_traits {};

template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) >
// we specialize for pointers to member function
{
    using result_type = ReturnType;
    using FunctionsType = ReturnType(ClassType::*)(Args...);

    using arg_tuple = std::tuple<Args...>;
    static constexpr auto arity = sizeof...(Args);
};

template <typename F>
struct lambda_expression_my
{
    typedef function_traits<decltype(&F::operator())> traits;
    typedef typename traits::result_type RT;
    typedef typename traits::FunctionsType FunctionType;

    F _object;
    FunctionType _function;

    lambda_expression_my(const F & object) :
    _object(object),
    _function(&F::operator()) {}

    template <typename ... Args>
    RT operator() (Args ... args) const {
        return (_object.*_function)(args...);
    }
};
“function\u traits”中没有名为“FunctionsType”的类型

#include <functional>
#include <iostream>

using namespace std;

template <typename T>
struct function_traits {};

template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) >
// we specialize for pointers to member function
{
    using result_type = ReturnType;
    using FunctionsType = ReturnType(ClassType::*)(Args...);

    using arg_tuple = std::tuple<Args...>;
    static constexpr auto arity = sizeof...(Args);
};

template <typename F>
struct lambda_expression_my
{
    typedef function_traits<decltype(&F::operator())> traits;
    typedef typename traits::result_type RT;
    typedef typename traits::FunctionsType FunctionType;

    F _object;
    FunctionType _function;

    lambda_expression_my(const F & object) :
    _object(object),
    _function(&F::operator()) {}

    template <typename ... Args>
    RT operator() (Args ... args) const {
        return (_object.*_function)(args...);
    }
};
#包括
#包括
使用名称空间std;
模板
结构函数{};
模板
结构功能特性
//我们专门提供指向成员函数的指针
{
使用结果类型=返回类型;
使用FunctionsType=ReturnType(类类型::*)(参数…);
使用arg_tuple=std::tuple;
静态constexpr auto arity=sizeof…(Args);
};
模板
结构lambda\u表达式\u my
{
类型定义功能特征;
typedef typename traits::result_type RT;
typedef typename traits::FunctionsType FunctionType;
F_对象;
函数类型_函数;
lambda_表达式_my(常数F和对象):
_对象(对象),,
_函数(&F::operator()){}
模板
RT运算符()(Args…Args)常量{
返回(_对象。*_函数)(参数…);
}
};

如果您的lambda不是
可变的
,您需要专门化
ReturnType(ClassType::*)(Args…)const的模板
@johanneschaub litb,像这样吗?然后我在构造函数中得到了常量错配:lambda\u表达式\u my(常量F&object):\u对象(object),\u函数(&F::operator()){}。使用functionType检查
行。@johanneschaub litb谢谢!)现在它使用FunctionsType=ReturnType(ClassType::*)(Args…)const工作;只要它适合结构的专门化(结构函数类型)!