c++ - how to get template parameter of a template template parameter? -


assume std::vector didnt have value_type. is possible write template deduce value_type? or more general, given t<x>, how can deduce x?

a naive..

template <template <typename x> t> void test(t<x> t) {       x x; } 

will make knows bit templates laugh @ foolish attempt , when instantiated this:

int main() {     std::vector<int> x;     test(x); } 

create following errors:

error: expected ‘class’ before ‘t’  template < template<typename x> t>                                  ^ error: ‘x’ not declared in scope  void test(t<x> u) {              ^ error: template argument 1 invalid  void test(t<x> u) {               ^ in function ‘void test(int)’: error: ‘x’ not declared in scope    x x;    ^ error: expected ‘;’ before ‘x’    x x;      ^ in function ‘int main()’: error: no matching function call ‘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>’) type ‘int’ 

edit: first 1 easy fix, fixing wont affect others...

ps: think have small misunderstanding, std::vector<int> isnt template, concrete type. however, still know if there way int sometemplate<int> template magic.

you can create traits extract 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, have 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; };  // ... 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -