dimanche 28 octobre 2018

What does `class template Example

I've been referred to http://www.cplusplus.com/articles/1C75fSEw/ which gives the following example:

template <typename T> class Example
{
public:
    Example( T test )
    {
        _data = test;
    }
    void setTest(T test)
    {
        _data = T;
    }
private:
    T _data;
};

class template Example<int>;
class template Example<float>;
class template Example<double>;

Apart from what looks like an omission error to me where a type is attempted to be assigned to a member variable -- _data = T instead of what I assume should be _data = test -- what I don't understand is what do the last 3 lines declare or instruct the compiler to do, exactly?

I've tried to compile the snippet using g++ -std=c++11 -pedantic and it compiles just fine (I corrected the omission error above first).

This came after I commented on the following answer: https://stackoverflow.com/a/14138629/254343 and I am still unsure whether either of the last 3 lines in the snippet is a template specialization or instantiation.

I also tried to grok this by attempting to understand the somewhat convoluted language of C++11 draft published by ISO at http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf but were unsuccessful as I can't deduce what class template ... would mean here.

Aucun commentaire:

Enregistrer un commentaire