Tuesday 1 October 2013

Template function default parameter and type inference

Template function default parameter and type inference

C++
None of these template functions
template<typename T> void foo(T par = nullptr) {return;} //#1
template<typename T> void foo(T par = std::nullptr_t(nullptr)) {return;}
//#2
template<typename T> void foo(T par = int(0)) {return;} //#3
allow anything with the following zero-argument call to compile:
foo();
although calling foo with any value works (e.g. foo(2)).
nullptr has a specific type, which is std::nullptr_t, so I didn't think
the default parameter needed extra type qualification in #1. The type of
the default parameter is explicitly provided in #2 and #3, so I didn't
think there was any type ambiguity there.
What is wrong here? Is there a proper way to do default parameters with
template functions?

No comments:

Post a Comment