Is there typedef template?

P

PengYu.UT

Hi,

I'm wondering if it is possible to define typedef template? For
example, I have template class A. I want give it an alias such as B.

The problem comes from refactoring the code. Suppost A is not a
template class. After refactoring, I decide to add a template
parameter to it.

But the original following code need to be updated as well.
typedef A B;

However, I don't find any easy way to change such code.

Thanks,
Peng

#include <iostream>

template <typename T>
class A {
public:
A() : _a(10.5) { }
T the_a() const { return _a; }
private:
T _a;
};

typedef int T;

template <typename T>
typedef A<T> B; // want to define a typedef for a class template

int main() {
T t(10);
std::cout << t << std::endl;

A_T<int> a(10);
std::cout << a.the_a() << std::endl;
}
 
A

Alf P. Steinbach

* (e-mail address removed):
Hi,

I'm wondering if it is possible to define typedef template? For
example, I have template class A. I want give it an alias such as B.

The problem comes from refactoring the code. Suppost A is not a
template class. After refactoring, I decide to add a template
parameter to it.

But the original following code need to be updated as well.
typedef A B;

However, I don't find any easy way to change such code.

Thanks,
Peng

#include <iostream>

template <typename T>
class A {
public:
A() : _a(10.5) { }
T the_a() const { return _a; }
private:
T _a;
};

typedef int T;

template <typename T>
typedef A<T> B; // want to define a typedef for a class template

int main() {
T t(10);
std::cout << t << std::endl;

A_T<int> a(10);
std::cout << a.the_a() << std::endl;
}

Template typedefs are part of the next standard.

Until then, you can wrap a template typedef in a struct,

template< typename T >
struct B { typedef A<T> Type; };

int main() { B<int>::Type b(10); }

Cheers, & hth.,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top