Assume std::vector
didnt have a value_type
. Is is possible to write a template to deduce the value_type
? Or more general, given a T<X>
, how can I deduce X
?
A very naive..
template <template <typename X> T>
void test(T<X> t) {
X x;
}
will probably make anybody who knows a bit about templates laugh at my foolish attempt and when instantiated like this:
int main() {
std::vector<int> x;
test(x);
}
create the following errors:
error: expected ‘class’ before ‘T’
template < template<typename X> T>
^
error: ‘X’ was not declared in this scope
void test(T<X> u) {
^
error: template argument 1 is invalid
void test(T<X> u) {
^
In function ‘void test(int)’:
error: ‘X’ was not declared in this scope
X x;
^
error: expected ‘;’ before ‘x’
X x;
^
In function ‘int main()’:
error: no matching function for call to ‘test(std::vector<int>&)’
test(x);
^
note: candidate is:
note: template<template<class X> class T> void test(int)
void test(T<X> u) {
^
note: template argument deduction/substitution failed:
note: cannot convert ‘x’ (type ‘std::vector<int>’) to type ‘int’
EDIT: the first one is easy to fix, but fixing it wont affect the others...
PS: I think I have a small misunderstanding, as std::vector<int>
isnt a template, but a concrete type. However, still I would like to know if there is a way to get the int
from a someTemplate<int>
with some template magic.
A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)
For example, given a specialization Stack<int>, “int” is a template argument. Instantiation: This is when the compiler generates a regular class, method, or function by substituting each of the template's parameters with a concrete type.
In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.
8. Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.
A template argument for a type template parameter must be a type-id, which may name an incomplete type: A template argument for a template template parameter must be an id-expression which names a class template or a template alias. When the argument is a class template, only the primary template is considered when matching the parameter.
Every template is parameterized by one or more template parameters, indicated in the parameter-list of the template declaration syntax: template < parameter-list > declaration Each parameter in parameter-list may be: a non-type template parameter;
A non-type template parameter must have a structural type, which is one of the following types (optionally cv-qualified, the qualifiers are ignored): the types of all base classes and non-static data members are structural types or (possibly multi-dimensional) array thereof.
If the default is specified for a template parameter of a primary class template , primary variable template, (since C++14)or alias template, each subsequent template parameter must have a default argument, except the very last one may be a template parameter pack.
You can create a traits to extract those parameter:
template <typename T> struct first_param;
template <template <typename, typename...> class C, typename T, typename ...Ts>
struct first_param<C<T, Ts...>>
{
using type = T;
};
For pre C++11, you have to handle number of parameters until acceptable values:
template <typename T> struct first_param;
template <template <typename> class C, typename T>
struct first_param<C<T>>
{
typedef T type;
};
template <template <typename, typename> class C, typename T, typename T2>
struct first_param<C<T, T2>>
{
typedef T type;
};
template <template <typename, typename, typename> class C,
typename T, typename T2, typename T3>
struct first_param<C<T, T2, T3>>
{
typedef T type;
};
// ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With