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 3521 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<typena me Container, typename Container::valu e_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<typena me Container, typename Container::valu e_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<templa te<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<typena me 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<typena me 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<templa te<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<templa te<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<typen ame Container, typename Container::valu e_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<templa te<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 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. i would
forget overriding it but i have to do it because its a coursework. here
is a simple version of the class
#include <iostream>
#include...
|
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 ownership expectation on auto_pointer.
|
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 dt
{
std::deque< class dt > dtdq;
};
| |
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>
template<class TObject>
|
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 Tparent::Tptr Tptr;
Consider example:
|
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: 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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |