Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typedefs for templated classes?

Is it possible to typedef long types that use templates? For example:

template <typename myfloat_t>
class LongClassName
{
    // ...
};

template <typename myfloat_t>
typedef std::vector< boost::shared_ptr< LongClassName<myfloat_t> > > LongCollection;

LongCollection<float> m_foo;

This doesn't work, but is there a way to achieve a similar effect? I just want to avoid having to type and read a type definition that covers almost the full width of my editor window.

like image 294
mch Avatar asked Oct 30 '08 19:10

mch


People also ask

Can templates be used for classes?

A template is not a class or a function. A template is a “pattern” that the compiler uses to generate a family of classes or functions. In order for the compiler to generate the code, it must see both the template definition (not just declaration) and the specific types/whatever used to “fill in” the template.

What are templated classes?

Definition. As per the standard definition, a template class in C++ is a class that allows the programmer to operate with generic data types. This allows the class to be used on many different data types as per the requirements without the need of being re-written for each type.

What is the syntax for class template?

What is the syntax of class template? Explanation: Syntax involves template keyword followed by list of parameters in angular brackets and then class declaration. As follows template <paramaters> class declaration; 2.

What is class template example?

A class template can be declared without being defined by using an elaborated type specifier. For example: template<class L, class T> class Key; This reserves the name as a class template name.


2 Answers

No, that isn't possible currently. It will be made possible in C++0X AFAIK.

The best I can think of is

template<typename T> struct LongCollection {
    typedef std::vector< boost::shared_ptr< LongClassName<T> > > type;
};

LongCollection<float>::type m_foo;
like image 170
Leon Timmermans Avatar answered Sep 19 '22 01:09

Leon Timmermans


If you don't want to go the macro way you have to make individual typedefs for each type:

typedef std::vector< boost::shared_ptr< LongClassName<float> > > FloatCollection;
typedef std::vector< boost::shared_ptr< LongClassName<double> > > DoubleCollection;
like image 21
activout.se Avatar answered Sep 20 '22 01:09

activout.se