473,473 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Template functions & class templates syntax

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
2 2472
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
4
by: Gert Van den Eynde | last post by:
Hi all, A beginners question.... I've got a template class template <class T> classA {...} In an other class, I want to pass a pointer to an instance of classA as a function argument....
4
by: DaKoadMunky | last post by:
My question relates to storing the addresses of functions generated by templates in pointers to functions. <CODE> template<class T> void Global() {} namespace ANamespace {
5
by: Steve | last post by:
Hi, Does C++ allow the programmer to declare a template with in a template so that a generic function can instantiate the embedded template? For example, could code such as this exist: ...
3
by: sks | last post by:
Hello all Is the usage of extern keyword valid for telling the compiler to NOT instantiate a template and to link it from an another binary? For example: Suppose module A's binary contains a...
4
by: Joseph Turian | last post by:
Hi, What is the correct syntax to get the bar<T>::f<int, unsigned>() function to compile in the following fragment? Thanks, Joseph class foo {
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.