templated typedef?
You can use C++11 templated type aliasing using using
e.g. like this
template <typename T>
using gc_vector = std::vector<T, gc_allocator<T>>;
Note: I know this is an old question but since it has quite many upvotes and as it turns up in search results I thought it deserved an updated answer.
C++ template typedef
C++11 added alias declarations, which are generalization of typedef
, allowing templates:
template <size_t N>
using Vector = Matrix<N, 1>;
The type Vector<3>
is equivalent to Matrix<3, 1>
.
In C++03, the closest approximation was:
template <size_t N>
struct Vector
{
typedef Matrix<N, 1> type;
};
Here, the type Vector<3>::type
is equivalent to Matrix<3, 1>
.
C++ templated typedef
You can use an inner type:
template <typename U>
struct sptr {
typedef tr1::shared_ptr<U> t;
};
Then say sptr<U>::t
, or unfortunately often typename sptr<U>::t
.
C++0x has template typedefs, you could check whether your compiler can be persuaded to accept them:
template<typename U>
using sptr = tr1::shared_ptr<U>;
Then say sptr<U>
And of course there's always #define sptr ::tr1::shared_ptr
, for example if you're expecting C++0x in future and want to bridge the gap. Or if you're using it in a narrow enough context that a macro isn't scary.
Why there is no templated typedef in C++11?
Quoting from the proposal Templates aliases for C++ N1489 (Emphasis Mine):
It has been suggested to (re)use the keyword
typedef
— as done in the paper [4] — to introduce template aliases:template<class T>
typedef std::vector<T, MyAllocator<T> > Vec;
That notation has the advantage of using a keyword already known to
introduce a type alias. However, it also displays several
disavantages among which the confusion of using a keyword known to
introduce an alias for a type-name in a context where the alias does
not designate a type, but a template;Vec
is not an alias for a
type, and should not be taken for atypedef
-name. The nameVec
is a
name for the familystd::vector<., MyAllocator<.>>
– where the bullet is a
placeholder for a type-name. Consequently we do not propose the “typedef
”
syntax.[4] Herb Sutter, Typedef templates, document no. WG21/1406.
How do typedefs work with templates?
When you typedef A B
you are simply saying that B is another name for A.
Do all 3 typedefs contain area, height, and width variables?
Yes. The squareTemplate
template class is defined to have area
, height
, and width
and all instantiations of it will have those members. In the case of your typedefs:
typedef squareTemplate <std::vector<std ::pair<double, double>>, std::vector<std::pair<int, int>>> squareRange;
area
has the type assumed by the first template parameter D
, so std::vector<std ::pair<double, double>>
; The same goes for height
and width
- they have the type of the second template argument, std::vector<std::pair<int, int>>
Following the same reasoning, you get:
typedef squareTemplate <bool, bool> squareValid;
all of them are bool
typedef squareTemplate<double, int> squareValue;
area
is int
; height
and width
are double
Templated typedef structs with constructor in C++
There is no need for a typedef struct
declaration in C++. This is a relic of C.
In C++, after declaring a struct ResourceHeader_s
, or struct ResourceHeader_t
, or just a plain struct ResourceHeader
, the same symbol can be used directly by itself, without an explicit struct
.
How to use a templated typedef on capturing lambda parameter?
L
is decalred as function pointer type. Lambdas without capture list could convert to function pointer implicitly, but lambdas with capture list can't.
This user-defined conversion function is only defined if the capture list of the lambda-expression is empty.
You can use std::function
instead.
template<typename T>
using L = std::function<T(T)>;
LIVE
BTW: You can use std::type_identity
(since C++20) to exclude the 2nd function argument of applyLambda
from deduction (see non deduced context for details), then template argument deduction would work for you and no need to specify the template argument again. This is true for the function pointer version too. (If your compiler doesn't support C++20 it's quite easy to make your own type_identity
.) E.g.
template<typename T>
T applyLambda(T var, std::type_identity_t<L<T>> lambda){
return lambda(var);
}
then
int b = applyLambda(3, [=] (int x)->int {
return x+a;
});
LIVE
Is a typedef to self allowed over template parameters
No. The name of a template parameter is not allowed to be redeclared.
The name of a template parameter is not allowed to be redeclared
within its scope (including nested scopes). A template parameter is
not allowed to have the same name as the template name.template<class T, int N>
class Y {
int T; // error: template parameter redeclared
void f()
{
char T; // error: template parameter redeclared
}
};
template<class X> class X; // error: template parameter redeclared
From the standard, [temp.local]/6:
The name of a template-parameter shall not be bound to any following
declaration contained by the scope to which the template-parameter
belongs. [Example 5:template<class T, int i> class Y {
int T; // error: template-parameter hidden
void f() {
char T; // error: template-parameter hidden
}
friend void T(); // OK: no name bound
};
template<class X> class X; // error: hidden by template-parameter— end example]
Related Topics
How to Use Threads to Speed Up File Reading
Lru Implementation in Production Code
Exclude Source File in Compilation Using Makefile
Check the File-Size Without Opening File in C++
Error: Passing 'Const …' as 'This' Argument of '…' Discards Qualifiers
Why Is Std::Is_Pod Deprecated in C++20
Using a Static Library in Qt Creator
C++ Implementing Timed Callback Function
How to Install C++ Plugin to Eclipse
Large 2D Array Gives Segmentation Fault
Using Condition Variable in a Producer-Consumer Situation
Returning Std::String/Std::List from Dll
Using Emit VS Calling a Signal as If It's a Regular Function in Qt
Why Do Some People Prefer "T Const&" Over "Const T&"
What Is the Rationale for Limitations on Pointer Arithmetic or Comparison
C++ Most Efficient Way to Convert String to Int (Faster Than Atoi)