3
template <typename Function> void for_each_element(
  const boost::tuples::null_type&, Function) {}

template <typename Tuple, typename Function> void     
  for_each_element(Tuple& t, Function func) {
    func(t.get_head());
    for_each_element(t.get_tail(),func);
}

Given the above code snippet, do we define a overload function or a partially specialized function?

Thank you

q0987
  • 34,938
  • 69
  • 242
  • 387
  • Neither of those are functions; they're templates. So it's a template overload, I suppose. – Thomas May 19 '12 at 19:30
  • @Thomas: Overload resolution *does* consider template instantiations. Function templates provide a whole (unbounded) *family* of overload candidates. – Kerrek SB May 19 '12 at 19:32
  • @thomas: both of those are functions. – Daniel May 19 '12 at 19:32
  • @Dani: No, both are function *templates*. – Kerrek SB May 19 '12 at 19:32
  • My prof was very keen on this distinction. You can only begin to comprehend template (meta)programming once you realize that a template is a *template*, a thing that can be used to "manufacture" classes/functions, but very different and distinct from its "products". – Thomas May 19 '12 at 19:36
  • @Thomas That distinction seems quite pedantic. I'm not sure what clarity it brings to the discussion. – Peter Wood May 19 '12 at 20:33

3 Answers3

5

There's no such thing as a function partial specialisation. It's an overload.

e.g.

template <typename T, typename U>
void foo(T t, U u);

template <typename T>
void foo<T, int>(T t, int u); // Illegal: no partial specialisation of functions

template <typename T>
void foo(T t, int u); // OK

Be careful when mixing specialisations with overloads, as it doesn't always work the way you may think it does.

Community
  • 1
  • 1
Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
0

Overloading. You cannot partially-specialize functions, and even if you could, you'd have a second pair of <>-brackets.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

It's an overload. Partial specialization of functions is not possible. If you try to partially specialize a function the compiler will complain. In this case, when you reach the end of the tuple, the compiler uses overload resolution to choose the first function.

Chris Hayden
  • 1,104
  • 6
  • 6
  • 1
    "The compiler will complain" is a really useless argument, and it can be used for anything: "If you say `class X / 2;`, the compiler will complain." is not an argument for or against anything but "you didn't write correct code". – Kerrek SB May 19 '12 at 19:31
  • What I was trying to point out is that it's disallowed by the standard, so even if you produce something that looks like a reasonable partial specialization for a function, the compiler will complain. Maybe I should have been more clear. – Chris Hayden May 19 '12 at 19:41
  • It's just better to argue purely in terms of the language, since mentioning any given compiler doesn't add anything. People often say things like "what do I have to do to make this compile", or "why is the compiler complaining about XYZ", or "fixing compilation errors" -- but that's all beside the simple point which is "what is the correct way to do X". As is so often the case, less is more! :-) – Kerrek SB May 19 '12 at 19:43