简体   繁体   English

如何专门化在可变参数模板 class 中定义的可变参数方法?

[英]How to specialized a variadic argument method defined inside a variadic templated class?

I have a variadic templated class which contains a method with variadic arguments.我有一个可变参数模板 class,其中包含一个可变参数 arguments 的方法。 I would like to specialize the method depending on some of the parameters provided with a specialized version of the class.我想根据 class 的专用版本提供的一些参数对方法进行专门化。

I know how to specialize variadic argument functions and how to perform template specialization.我知道如何专门化可变参数函数以及如何执行模板专门化。 Unfortunately, I have not managed to use both specializations together.不幸的是,我没有设法同时使用这两个专业。

My current solution seems to be overriding the solution which is not what I want.我当前的解决方案似乎覆盖了我不想要的解决方案。 Below is the simplified problem下面是简化的问题

    #include <iostream>

struct X;
struct Y;

template<typename ... FooTs>
struct Foo
{
public:

  template < typename... Ts >
  static int value(Ts... args){ return 0;};

};

template <>
struct Foo<X,Y>{
    static int value(int& a, int& b, float& c)
    {
        std::cout << "specialized value 3 args..." << std::endl;
        return 0;
    }
};

/* Ideally I would also like to have such specialization
template <>
struct Foo<X,Y>{
    int value(int a, int b)
    {
        std::cout << "specialized value 2 args..." << std::endl;
        return 0;
    }
};*/

int main(){
    Foo<X, Y> foo;
    int a = 1;
    int b = 2;
    float c = 3.4;
    Foo<X,Y>::value(a, b, c);
    foo.value(a, b, c);
    // foo.value(a, b);  // error: no matching function for call to 'Foo<X, Y>::value(int&, int&)
    return 0;
}

How can I achieve the specialization of the "value" method on the example above?如何在上面的示例中实现“价值”方法的专业化?

There can only be be single Foo<X,Y> type.只能有一个Foo<X,Y>类型。

Because of this, you cannot specialize Foo itself based on the template parameters of one of its functions.正因为如此,您不能根据其函数之一的模板参数专门化Foo本身。 Think about it: What would happen if Foo had multiple templated functions inside of it?想一想:如果Foo里面有多个模板函数会发生什么?

However, you can create a specialized version of the function like you are asking by overloading the function itself instead of the type:但是,您可以通过重载function本身而不是类型来创建 function 的专用版本,就像您要求的那样:

template<>
template<>
int Foo<X, Y>::value(int a, int b) {
    return 12;
}

int test() {
    return Foo<X,Y>::value(1,2);
}

This has little to do with templates, or specialization.这与模板或专业化无关。 It's simple, really:很简单,真的:

template <>
struct Foo<X,Y>{
    static int value(int& a, int& b, float& c)
    {
        std::cout << "specialized value 3 args..." << std::endl;
        return 0;
    }
    int value(int a, int b)
    {
        std::cout << "specialized value 2 args..." << std::endl;
        return 0;
    }
};

Demo演示

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

相关问题 如何在声明之外定义一个专用于无参数的可变参数模板类的C ++方法? - how to define a C++ method outside the declaration for a variadic templated class specialized to no parameters? 使用模板化可变参数模板参数作为专用参数 - Use a templated variadic template parameter as specialized parameter C++17 - 可变参数模板 arguments - 使用模板参数调用 class 方法返回值 - C++17 - variadic template arguments - call class method on return value from previous with templated argument 获得可变模板类的第N个参数的最简单方法是什么? - Easiest way to get the N-th argument of a variadic templated class? “继承”具有可变模板函数的类 - “Inheriting” a class with variadic templated function 带有可变参数包的模板化可添加 class - templated addable class with variadic pack 模板化类中运算符[]的可变参数化模板重载 - variadic templated overload of operator [] in templated class 使用专门的可变参数模板类的声明 - using declaration of a specialized variadic template class 用于类构建的 2 个模板化构造函数与可变参数模板的组合。 如何? - Combination of 2 templated constructors for class build with variadic templates. How? 如何以简单的方式声明可变参数模板类的类型 - How to declare the type of a variadic templated class in a simple way
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM