简体   繁体   English

模板 Class 类型和功能的专业化添加

[英]Template Class Specialization Addition of Types and Functions

I have been learning templates and templates specializations.我一直在学习模板和模板专业。 I want to be able to add two template parameters together.我希望能够将两个模板参数一起添加。 I was able to do this with basic types, but could this be done with functions as well with the use of a lambda function and a class specialization.我可以用基本类型来做到这一点,但是这也可以通过使用 lambda function 和 class 专业化来完成。

The following code with not compile:以下代码未编译:

template<class R>
class Adder
{
public:
    template<R A, R B>
    static R Add() { return A + B; }
protected:
};

template<class R, class ...Args>
class Adder<std::function<R(Args...)>>
{
public:
    template<std::function<R(Args...)> A, std::function<R(Args...)> B>
    static std::function<R(Args...)> Add()
    {
        std::function<R(Args...)> Func = [](Args...) -> R { return A(Args...) + B(Args...); };
        return Func;
    }
protected:
};


double Test1(double x)
{
    return x;
}

int main()
{

    int a = Adder<int>::Add<2,2>();

    std::function<double(double)> Func = Adder<std::function<double(double)>>::Add<Test1, Test1>();

    return 0;
}

I get the following errors:我收到以下错误:

error C2993: 'std::function<double (double)>': is not a valid type for non-type template parameter 'A'
message : 'function<double __cdecl(double)>' is not a literal class type
message : see reference to class template instantiation 'Adder<std::function<double (double)>>' being compiled
error C2993: 'std::function<double (double)>': is not a valid type for non-type template parameter 'B'
message : 'function<double __cdecl(double)>' is not a literal class type
error C2672: 'Adder<std::function<double (double)>>::Add': no matching overloaded function found
error C2993: 'std::function<double (double)>': is not a valid type for non-type template parameter 'A'
error C2993: 'std::function<double (double)>': is not a valid type for non-type template parameter 'B'

Based on my limited mad man experience I would think this class would be possible, but I wasn't able to find much information on this specific topic.根据我有限的疯子经验,我认为这个 class 是可能的,但我无法找到关于这个特定主题的太多信息。 I'm sure the answer is in the error messages.我确定答案在错误消息中。

This will compile and works as expected... Adds the Types or Functions.这将按预期编译和工作...添加类型或函数。

template<class R>
class Adder
{
public:
    template<R A, R B>
    static R Add() { return A + B; }
protected:
};

template<class R, class ...Args>
class Adder<std::function<R(Args...)>>
{
public:
    template<R Func1(Args...), R Func2(Args...)>
    static std::function<R(Args...)> Add()
    {
        return [](Args... Vars) -> R { return Func1(Vars...) + Func2(Vars...); };
    }
protected:
};

int Test1(int x)
{
    return x;
}

int main()
{
    int a1 = Adder<int>::Add<1, 1>();
    std::function<int(int)> F1 = Adder<std::function<int(int)>>::Add<Test1, Test1>();
    int V1 = F1(1);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM