20

Does anyone know whether, in C++11, function templates can be partially specialized?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194

2 Answers2

15

No, they can't. The draft C++0x standard has a section (14.5.5) on class template partial specialisations, but no mention of function template partial specialisations.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • It was my understanding that this is in C++0x. – Puppy Sep 15 '10 at 10:57
  • @DeadMG: Yes, I'm referring to the draft C++0x standard, http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3092.pdf – Mike Seymour Sep 15 '10 at 11:08
  • FYI: The latest draft is now [N3126](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3126.pdf) [Warning: Big PDF]. The answer isn't different in the latest draft. – James McNellis Sep 15 '10 at 11:20
  • The Feb 2011 draft again makes no mention of them: [N3242](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf) (10Mb) – user2023370 Mar 17 '11 at 12:09
-1

No; they were proposed as core language issue #229 (from n1295) but ultimately rejected (and quite rightly so, since overloading does the job).

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 17
    I have to disagree with "quite rightly so". Overloading does not always do the job since we can't have overloads that differ only on return type. It would be nice if I could do something like the following, for example: template T& Foo() { ... } template void Foo() { ... } – Peter Ruderman Oct 01 '14 at 16:31
  • I agree overloading is not sufficient. Say I want to overload std::make_shared for a legacy C struct with custom create_* and destroy_* functions. A partial specialization would be very useful in this case. – AndyJost Oct 05 '15 at 21:22
  • 4
    More disagreement to "quite rightly so": `enable_if`. If we could partially specialize functions, we could avoid clumsy hacks with tag helpers. – GreenScape Jun 01 '16 at 10:40