LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-08-2004, 12:44 AM   #1
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Rep: Reputation: 30
Using typename


Hi.


Just wanted some general info about typename. Why it is used, what is does and how it is used - the works. I know about typedef and I think they're linked somehow.


Thanks.
 
Old 07-08-2004, 05:38 AM   #2
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
I would say linked through common usage together but not really in function.

In a nutshell, it is used in template declarations and defintions where you cannot determine if an identifier is a type or value.

See these two threads:

http://www.linuxquestions.org/questi...light=typename

http://www.linuxquestions.org/questi...light=typename

The second is much more informative but the first gives some context.

There is a bit more to it than this in practice but post back if you have more questions after reading.

Last edited by dakensta; 07-08-2004 at 05:46 AM.
 
Old 07-09-2004, 07:45 AM   #3
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
Thanks. I will.
 
Old 07-11-2004, 02:22 PM   #4
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
Hi.

I do have some questions. Like what does the guy mean by DB::i? And can you please elaborate what you mean by when you don't know whether an argument is a value of a type. Please give some examples. Thankyou.
 
Old 07-12-2004, 09:33 AM   #5
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Quote:
Code:
class DB
{
public:
 static int i;
}
... OR ...
Code:
class DB
{
public:
  typedef int i;
}
(corrected slightly)

DB::i will give you access to whatever ' i ' is. However, the same syntax is used to access both, DB::i does not tell me if i is a value or a type, the first or the second case above.

In the first case ' i ' is a value, a class scope integer that can be accessed through the use of the scope resolution operator ::

You can write:

DB::i = 100;

With the second example, ' i ' is a type. You can write code that will declare an integer like this:

DB::i value = 100;

However, sometimes this second case presents a problem to the compiler. In theory, we humans can tell the difference quite easily, but not quite so for the compiler which may not have access to the whole template definition and cannot jump about the code like humans can and needs to produce nice error messages that are vaguely readable in a timeframe considerable shorter than a week.

So the second example may require a prefix of typename:

typename DB::i value = 100;

Hence, typename gives the difference between a value and a type.

When do you need to do something like this, though?
Answer : In a template.

Consider writing a function to print the maximum value of any container.
We want to get a temporary value of whatever type is contained in that container.

Luckily, in the standard library, the containers provide us with a typedef called value_type.
More than that, they also provide us with a typedef that gives us 'something' to iterate over the container with, called an iterator.

We can now write a function like this:
Code:
template<typename container_t>
void
show_me_the_biggest(const container_t& c)
{
  typename container_t::const_iterator i = c.begin();
  typename container_t::value_type max = *(i++);

  for (; i != c.end(); ++i)
    if (*i > max) max = *i;

  cout << "Maximum is : " << max << endl;
}
We have to tell the compiler that 'iterator' and 'value_type' are like the SECOND case in the quote above, i.e. types.

Although, just to complicate matters (because life is never simple) you do not need the typename keyword if you are not in a template, if you were to write the same code in a non-generic form.
As a rule of thumb, if you can read the code on your screen and determine the entire type of the template (i.e. what all the parameters are) then you don't need typename.


The opposite example is a template function for any types with a common static value defined (but none spring to mind at the moment!)

Last edited by dakensta; 07-12-2004 at 10:46 AM.
 
Old 07-13-2004, 03:21 AM   #6
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
Thanks for the info. Just one thing, value_type is defined only within the STL, right? So this example is applicable only to the STL containers, right? Otherwise, I get it. Thanks again.
 
Old 07-13-2004, 04:32 AM   #7
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Yes (I don't have a copy of the standard right here but I am pretty sure it is a requirement), but that said value_type crops up all over the place e.g. boost (http://www.boost.org) and is worth adding to any container you might write even if it is purely from a readability or convenience point of view (people use all sorts of identifiers in templates, e.g. whatever_t, T, TYPE, T_Whatever)

Bear in mind that the more consistent (ideally with the standard library) your own class' interface, the easier it is to write templated code and reuse standard algorithms etc.

HTH
 
Old 07-14-2004, 04:09 AM   #8
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
Ok. Thanks.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Implicit typename warning (gcc 3.3.4) ta0kira Programming 1 10-12-2004 03:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration