By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,849 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,849 IT Pros & Developers. It's quick & easy.

Template functions & class templates syntax

P: n/a
Hi!

Suppose I have a class template Foo
with a member function template bar - so far so good:

template <typename T>
struct Foo {
template <typename U>
void bar(const U& u);
};

No problem so far. Now, I want to define bar. This doesn't work:

template <typename T, typename U>
void Foo<T>::bar(const U& u) {
}

It cost me half an hour of trial-and-error to find out
that this does work:

template <typename T> template <typename U>
void Foo<T>::bar(const U& u) {
}

I thought U was just an additional template parameter,
but it seems like two nested template declarations are needed -
why ?

thanks in advance & kind regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Frank Schmitt wrote:
Hi!

Suppose I have a class template Foo
with a member function template bar - so far so good:

template <typename T>
struct Foo {
template <typename U>
void bar(const U& u);
};

No problem so far. Now, I want to define bar. This doesn't work:

template <typename T, typename U>
void Foo<T>::bar(const U& u) {
}

It cost me half an hour of trial-and-error to find out
that this does work:

template <typename T> template <typename U>
void Foo<T>::bar(const U& u) {
}

I thought U was just an additional template parameter,
but it seems like two nested template declarations are needed -
why ?


Because !

The correct answer is that it just "IS" that way.

However if you're looking for philosophy, I can imagine that the
creators of the standard nested templates and decided that the best way
to represent these in the second form was to show the nesting. I can
also imagine there would be ambiguity in the syntax if alternatives were
used.


Jul 22 '05 #2

P: n/a
Frank Schmitt wrote:
Hi!

Suppose I have a class template Foo
with a member function template bar - so far so good:

template <typename T>
struct Foo {
template <typename U>
void bar(const U& u);
};

No problem so far. Now, I want to define bar. This doesn't work:

template <typename T, typename U>
void Foo<T>::bar(const U& u) {
}

It cost me half an hour of trial-and-error to find out
that this does work:

template <typename T> template <typename U>
void Foo<T>::bar(const U& u) {
}

I thought U was just an additional template parameter,
but it seems like two nested template declarations are needed -
why ?


Because that's what it is. A nested template. Not a single template with
two parameters.

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.