Is there any difference between declaring a template parameter as a
"typename" or a "class"? E.g.
template <class TT f() { }
template <typename TT g() { }
Thanks,
Jason 10 3513 ja************@gmail.com wrote:
Is there any difference between declaring a template parameter as a
"typename" or a "class"? E.g.
No. They are equivalent. ja************@gmail.com wrote:
Is there any difference between declaring a template parameter as a
"typename" or a "class"?
Yes: 'class' is more confusing because the type might not actually be
a class at all.
Juha Nieminen wrote:
ja************@gmail.com wrote:
>Is there any difference between declaring a template parameter as a "typename" or a "class"?
Yes: 'class' is more confusing because the type might not actually
be a class at all.
And typename is more confusing because it has other uses as well. :-)
template<typename Container, typename Container::value_type>
class something;
One typename is the same as class, but not the other. Isn't that
confusing?
Bo Persson
On 2008-05-23 20:44, Bo Persson wrote:
Juha Nieminen wrote:
>ja************@gmail.com wrote:
>>Is there any difference between declaring a template parameter as a "typename" or a "class"?
Yes: 'class' is more confusing because the type might not actually be a class at all.
And typename is more confusing because it has other uses as well. :-)
template<typename Container, typename Container::value_type>
class something;
One typename is the same as class, but not the other. Isn't that
confusing?
template<class T>
class Foo { };
One class is the same as typename, but not the other. Isn't that
confusing? :-)
Seriously though, there is at least one instance where they are not
identical:
template<template<typename Uclass T>
class Foo { };
--
Erik Wikström ja************@gmail.com wrote:
Is there any difference between declaring a template parameter as a
"typename" or a "class"? E.g.
template <class TT f() { }
template <typename TT g() { }
As others have said, in this case "class" and "typename" have identical
semantics.
That said, I tend to use them both in templates as sort of
documentation, and a usage aid.
e.g.:
template<typename Tsomething... indicates that T can be any type
template<class Tsomething... indicates that T should be a class type.
red floyd wrote:
ja************@gmail.com wrote:
>Is there any difference between declaring a template parameter as a "typename" or a "class"? E.g.
template <class TT f() { } template <typename TT g() { }
As others have said, in this case "class" and "typename" have identical
semantics.
That said, I tend to use them both in templates as sort of
documentation, and a usage aid.
e.g.:
template<typename Tsomething... indicates that T can be any type
template<class Tsomething... indicates that T should be a class type.
Follow up note, to remove all confusion.
This is merely a *coding convention* that *I use*, it is not mandated by
the language.
Erik Wikström wrote:
template<template<typename Uclass T>
class Foo { };
Btw, is there any tutorial or other resources in the net which would
explain in detail all that can be done with templates?
Usually you can find just the basic usage and that's it.
Juha Nieminen wrote:
Erik Wikström wrote:
> template<template<typename Uclass T> class Foo { };
Btw, is there any tutorial or other resources in the net which would
explain in detail all that can be done with templates?
Usually you can find just the basic usage and that's it.
If you want that level of detain, get hold of a copy of the excellent
"C++ Templates" by Vandevoorde and Josuttis.
--
Ian Collins.
Erik Wikström wrote:
On 2008-05-23 20:44, Bo Persson wrote:
>Juha Nieminen wrote:
>>ja************@gmail.com wrote: Is there any difference between declaring a template parameter as a "typename" or a "class"?
Yes: 'class' is more confusing because the type might not actually be a class at all.
And typename is more confusing because it has other uses as well. :-)
template<typename Container, typename Container::value_type> class something;
One typename is the same as class, but not the other. Isn't that confusing?
template<class T>
class Foo { };
One class is the same as typename, but not the other. Isn't that
confusing? :-)
Seriously though, there is at least one instance where they are not
identical:
template<template<typename Uclass T>
class Foo { };
Yes, in some places you must use class, in some places you must use
typename, and in other places you can use either.
This really *is* confusing, and I believe that there are now some
regrets for allowing this. Seemed like a good idea at the time, but...
Bo Persson
On May 24, 11:07*am, Juha Nieminen <nos...@thanks.invalidwrote:
Erik Wikström wrote:
* template<template<typename Uclass T>
* class Foo { };
* Btw, is there any tutorial or other resources in the net which would
explain in detail all that can be done with templates?
* Usually you can find just the basic usage and that's it.
http://www.linuxtopia.org/online_boo...mming_104.html
Have fun it's more than you'll ever need This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: pub |
last post by:
When creating a list:
list<class A*> l;
How to delete all the objects whose pointers are contained in "l"?
Thanks for your comments?
|
by: franklini |
last post by:
hello people i. can anybody help me, i dont know what is wrong with
this class. it has something to do with the me trying to override the
input output stream. if i dont override it, it works fine....
|
by: ben |
last post by:
template <typename T>
class A
{
friend class T; // error, but why?
};
|
by: guru.slt |
last post by:
Hi,
see this code:
auto_ptr<int> int_auto_p(new int(3));
auto_ptr<int> int_auto_p2(int_auto_p.get());
Then, both auto_pointer will own the same object. That will violate the
single...
|
by: Dan Trowbridge |
last post by:
He everyone,
I am just getting started with .NET and I am having a porting problem.
I get and error in code that lookssomething like this (really stripped down
but you get the idea)...
class...
| |
by: SoilMan |
last post by:
Consider the following:
class xyz
{
public:
template <typename T>
void foo(T x)
{
cout << "foo<T> " << x << endl;
}
|
by: Arne Schmitz |
last post by:
Why do I get the following error:
error: type 'std::vector<TObject*, std::allocator<TObject*' is not
derived from type 'bar<TObject>'
with the following code:
#include <vector>
...
|
by: Grizlyk |
last post by:
Hello.
Why were base class "typedefs" hidden by template<and explicit usage
of them does not work too?
Try open only one of the lines in the example below
//using Tparent::Tptr;
//typedef...
|
by: Ed |
last post by:
Hi, guys,
Here is a simple template class definition:
template <typename T = int>
class Point {
public:
T X;
T Y;
T Z;
};
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |