1

Does the following default argument for the template instantiates a template with type EmptyClass?

class EmptyClass{};

template <typename TYPE=EmptyClass>
class Sample
{
public:
    static void test()
    {
        TYPE::Serialize();
    }
};
bogdan
  • 9,229
  • 2
  • 33
  • 48

2 Answers2

0

No. Template are instantiated when used, and are instantiated on a per-function base.

Default parameter values are just the types to use when the parameter is not specified. But does not themselves imply usage.

When you call Sample<>::test() then Sample<Emptyclass>::test() is instantiated and the EmptyClass::serialize() call attempted, resulting in a compile-time error (Since Emptyclass is declared as not having such function)

Try to make up more function, containing different compile-time errors referring to different parameter, and you'll see how, until the function is not used, no error is produced.

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63
-1

No, in that code any instance of EmptyClass is created. Serialize is a static function. And EmptyClass's constructor is never called (in code showed)

Ing. Gerardo Sánchez
  • 1,607
  • 15
  • 14
  • 1
    The question was not about constructors and objects. It is about template instantiation – Sabhareesh Ravichandran Mar 21 '16 at 13:49
  • 1
    Instance of a template is not an instance of a class which is an instance of a class template. So I do not underszand what your answer has to do with the question. And I can't underszand why this "answer" was excepted... – Klaus Nov 27 '16 at 15:17