Hi,

I found the examples of explicit specialization, but I have a vagueness about code:

using namespace std;

template<class T = float, int i = 5> class A
{
   public:
      A();
      int value;
};

template<> class A<> { public: A(); };
template<> class A<double, 10> { public: A(); };

template<class T, int i> A<T, i>::A() : value(i) {
   cout << "Primary template, "
        << "non-type argument is " << value << endl;
}

A<>::A() {
   cout << "Explicit specialization "
        << "default arguments" << endl;
}

A<double, 10>::A() {
   cout << "Explicit specialization "
        << "<double, 10>" << endl;
}

int main() {
   A<int,6> x;
   A<> y;
   A<double, 10> z;
}

What does this line of code do?

template<class T, int i> A<T, i>::A() : value(i)

My assumption is that we are creating general definition for A().
But what baffles me is the syntax of

A():value(i)

I remember of this from inheritance tutorial where we can specify which constructor of base class will be called together with constructor of derived class.

Can anyone explain me how does this work?

Thanks!

Recommended Answers

All 2 Replies

That syntax is the initialization list.

The initialization list is for all sub-objects (which includes both base-class instances and data members) of an object. It is simply a comma-separated list of constructor calls for each base-class instance and data member belonging to a class. In your specific example, value is a data member of the general class template A and it is constructed with value i.

That syntax is the initialization list.

The initialization list is for all sub-objects (which includes both base-class instances and data members) of an object. It is simply a comma-separated list of constructor calls for each base-class instance and data member belonging to a class. In your specific example, value is a data member of the general class template A and it is constructed with value i.

Thanks, helped!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.