yintao
yintao

Reputation: 23

why this code has error with template specialization

I want to use one function pass parameters to different class, so I use class template specialization and variadic function template, bellow is the code:

#include <iostream>
using namespace std;

template <typename T0, typename T1>
class Package
{
    T0 Data0;
    T1 Data1;
public:
    Package(T0 data0, T1 data1): Data0(data0), Data1(data1) {}
    void Display()
    {
        cout << Data0 << " " << Data1 << endl;
    }
};

template <typename T0>
class Package<T0, void>
{
    T0 Data0;
public:
    Package(T0 data0): Data0(data0) {}
    void Display()
    {
        cout << Data0 << endl;
    }
};

template <>
class Package<void, void>
{
public:
    Package() {}
    void Display()
    {
        cout << "have no member" << endl;
    }
};

template <typename... Args>
void Post(Args... args)
{
    Package<Args...> pak = Package<Args...>(args...);
    pak.Display();
}

int main()
{
    int x = 5;
    float y = 0.9;
    Post(x, y);
    Post(x);
    Post();
    return 0;
}

I have this error:

 In instantiation of 'void Post(Args ...) [with Args = {int}]': 
 52:15: required from here 
 43:26: error: wrong number of template arguments (1, should be 2) 
 5:11: error: provided for 'template<class T0, class T1> class Package' 
 43:26: error: wrong number of template arguments (1, should be 2) 
 5:11: error: provided for 'template<class T0, class T1> class Package' 
 In instantiation of 'void Post(Args ...) [with Args = {}]': 
 53:14: required from here 
 43:26: error: wrong number of template arguments (0, should be 2) 
 5:11: error: provided for 'template<class T0, class T1> class Package' 
 43:26: error: wrong number of template arguments (0, should be 2) 
 5:11: error: provided for 'template<class T0, class T1> class Package'

Package can take 2/1/0 parameters, Post can provide 2/1/0 parameters, but why has this error? thanks.

Upvotes: 0

Views: 1291

Answers (3)

Jarod42
Jarod42

Reputation: 217245

You use void instead of no types, you might change your class to:

template <typename ...Ts> class Package;

template <typename T0, typename T1>
class Package<T0, T1>
{
    T0 Data0;
    T1 Data1;
public:
    Package(T0 data0, T1 data1): Data0(data0), Data1(data1) {}
    void Display() const
    {
        std::cout << Data0 << " " << Data1 << std::endl;
    }
};

template <typename T0>
class Package<T0>
{
    T0 Data0;
public:
    explicit Package(T0 data0) : Data0(data0) {}

    void Display() const
    {
        std::cout << Data0 << std::endl;
    }
};

template <>
class Package<>
{
    void Display() const { std::cout << "have no member" << std::endl; }
};

Upvotes: 1

R Sahu
R Sahu

Reputation: 206577

Package can take 2/1/0 parameters, Post can provide 2/1/0 parameters, but why has this error?

The specializations help the compiler figuring out which one to use when the template is instantiated.

If you use

Package<void, void> p1;

the last specialization is used. If you use

Package<int, void> p1;
Package<double, void> p2;

the first specialization is used.

If you use

Package<int, float> p1;
Package<double, char> p2;

the original class template is used.

However, no matter which one is used, you still need two template parameters.

If you would like to be able to use

Package<int> p1;

you will have to provide a default type for the second parameter, as was suggested by the other answer.

template <typename T0 = void, typename T1 = void>
class Package { ... };

Upvotes: 0

max66
max66

Reputation: 66200

Try adding default template arguments to Package

template <typename T0 = void, typename T1 = void>
class Package

Upvotes: 1

Related Questions