I create the simple template structure like this

#include <iostream>

template <class info>
struct box
{
	info data;
};

int main()
{
	box<int> b1;
	b1.data = 1;
	std::cout<<b1.data;
	return 0;typedef box square;
}

Now i want to use the typedef for this template structure.
I could not figure out the syntax...
as i tried

typedef box square;

But i didn't workout... Please help me..:'(

Recommended Answers

All 12 Replies

I don't think it's in the language.
You could use #define square box.

You may want to google template typedefs, or check out http://www.gotw.ca/gotw/079.htm for some solutions.

I don't think it's in the language.
You could use #define square box.

You may want to google template typedefs, or check out http://www.gotw.ca/gotw/079.htm for some solutions.

Couldn't understand your like... but might come handy later... thanx:(

What's a problem?

typedef box<int> MyFavouriteBoxType;
...
MyFavouriteBoxType b1;

Look at STL headers on you C++ installation. There are lots of (useful) typedefs in STL templates...

yea, you gotta either macro it or typedef with specifying the actual template type :P sucks in a way... but hey, what's the deal with the box thing, it's pretty small and easy to write...
i use typedef for long, looooong names, like:

typedef my_monumental_structure<another_huge_type>::iterator sit;

and then i just sit... all the way... rocks

What's a problem?

typedef box<int> MyFavouriteBoxType;
...
MyFavouriteBoxType b1;

Look at STL headers on you C++ installation. There are lots of (useful) typedefs in STL templates...

Is there any possibilities to have generic type typedef. As normally typedef copies complete characteristics of it host.

yea, you gotta either macro it or typedef with specifying the actual template type :P sucks in a way... but hey, what's the deal with the box thing, it's pretty small and easy to write...
i use typedef for long, looooong names, like:

typedef my_monumental_structure<another_huge_type>::iterator sit;

and then i just sit... all the way... rocks

I didn't get what your syntax says. :confused:
Can you kindly explain it..

Typedef name is an alias for the type, it's not a new type name.
Can you explain what a problem are you trying to solve with "generic typedef"?

I believe he's trying to do something like:

typedef box MyGreatBeautifulBox;

//and use it as:
MyGreatBeutifulBox<int> a;
MyGreatBeutifulBox<char>b;

I don't think it can be done

Hehe...

template<typename T> 
struct MyGreatBeautifulBox:box<T>{};

int main()
{
    MyGreatBeautifulBox<int>  ibox;
    MyGreatBeautifulBox<char> cbox;

I didn't get what your syntax says. :confused:
Can you kindly explain it..

umm, sorry if it was not understandable, i tried to come up with a very long name for a type i want to define... so i made the name myself, don't try it it won't work. unless you define such a class. which would be silly... c++ isn't java :P

commented: Funny guy...:D +1

Typedef name is an alias for the type, it's not a new type name.
Can you explain what a problem are you trying to solve with "generic typedef"?

I got your point..
But lets say If i want to create the pointer variable.
like for example normally what we do is

struct box 
{
 ... blah .. blah.. blah
};

typedef box *crazy_box;
//as we can now easy create the pointer with crazy_box
crazy_box B1;

Now I want this to happen with the template structure what to do...

Hehe...

template<typename T> 
struct MyGreatBeautifulBox:box<T>{};

int main()
{
    MyGreatBeautifulBox<int>  ibox;
    MyGreatBeautifulBox<char> cbox;

Hey that Cheating. This is not what i meant for.. :sad:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.