-1

It would be like function overloading. E.g. it is ok to do this:

void foo(int i) {
  ;
}

// Function overload ftw.
void foo(int i, int j) {
  ;
}

But it is not (yet) ok to do this:

template<typename T>
class Foo {
};

// Fail!
template<typename T1, typename T2>
class Foo {
};

Does this feature not exist in order simply to avoid confusion? Or is there some reason this wouldn't actually make sense?

allyourcode
  • 21,871
  • 18
  • 78
  • 106

2 Answers2

1

No it is not possible in c++ to do that.

The template is looked up first, then the parameters which make it impossible to know which template is which.

It does seem like a duplicate to this one: Why is it not possible to overload class templates?

Community
  • 1
  • 1
Dory Zidon
  • 10,497
  • 2
  • 25
  • 39
  • I know it's not possible. My question is why not? Does this feature not make sense? Or has it simply not been spec'ed out? Based on the other question you (and CaptainObvious) point to, it seems to be simply a matter of never having been spec'ed out. Anyway, thank you for your answer! – allyourcode May 19 '13 at 01:45
1

It's not possible the way you wrote, but easy to do with partial specialization:

template <typename...> struct Foo;   // don't even define


template <typename T>
struct Foo<T>
{
    // ... "one-argument" version
};

template <typename T1, typename T2>
struct Foo<T1, T2>
{
    // ... "two-arguments" version
};
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084