Search code examples
c++templatesvariadic-templates

Prefect Forwarding in a template based class with variadic templates?


I try to implement a template base class. Which take a string and n number of params and base on string pass all given arguments to some function using prefect forwarding. I have written a sample code for this.

template <typename T, typename ... Args>
class temp {
    public:
        temp(){};
        ~temp(){};
        T main_fn(const std::string& a, Args&& ... args){
            if(a == "add"){
                return add(std::forward<Args>(args)...);
            }
            else if(a == "sub"){
                return sub(std::forward<Args>(args)...);
            }
            else{
                std::cout << "abc" << std::endl;
            }     
        }  
};

int main(){

  std::cout << std::endl;
  temp<int>* temp_obj = new temp<int>();
  const std::string fn = "add";
  int result = temp_obj->main_fn(fn, 1,2,3);
  std::cout << result << std::endl;

}

When i try to compile this code i got following error.

 In function 'int main()':
70:43: error: no matching function for call to 'temp<int>::main_fn(const string&, int, int, int)'
70:43: note: candidate is:
40:11: note: T temp<T, Args>::main_fn(const string&, Args&& ...) [with T = int; Args = {}; std::string = std::basic_string<char>]
40:11: note:   candidate expects 1 argument, 4 provided

Any help would be appreciated.


Solution

  • temp<int> has the member function main_fn(const std::string& a), because there are no further Args... in temp<int>.

    If you want to parametrize the class on T but the method on more Args... then you can write this:

    #include <iostream>
    #include <string>
    template <typename T>
    class temp {
        public:
            temp(){};
            ~temp(){};
            template <typename ... Args>
            T main_fn(const std::string& a, Args&& ... args){
                return 42;
            }  
    };
    
    int main(){
    
      std::cout << std::endl;
      temp<int>* temp_obj = new temp<int>();
      const std::string fn = "add";
      int result = temp_obj->main_fn(fn, 1,2,3);
      std::cout << result << std::endl;
    
    }