jbgs
jbgs

Reputation: 2885

Function Template Specialization Syntax

Given this function template:

template<typename A> void func (A param) {...}

In order to write an specialization, we can use either this idiom (A)

template<> void func<> (int param) {...}

Or this one (B)

template<> void func<int> (int param) {..}

Is there any difference between A and B? If both are the same, what's the preferred syntax and why?

Upvotes: 1

Views: 97

Answers (1)

txtechhelp
txtechhelp

Reputation: 6752

A.) template<> void func<> (int param) {...}

B.) template<> void func<int> (int param) {..}

Is there any difference between A and B?

Technically B has the template argument explicitly specified while A has it's argument reduced, but to the user it's merely how you present your function.

If both are the same, what's the preferred syntax and why?

Since defining both would result in a multiple definition error (i.e. they're the same function), it's really up to personal preference as to how you present the function.

I'm personally partial to B since it fully defines and clearly states intention of function, but again that's my personal preference

Hope that can help

Upvotes: 3

Related Questions