c++ - How to store template type in structure -
#include<iostream> #include<typeinfo> using namespace std; template <class key, class value> struct st { typedef key keytype; typedef value valuetype; }; int main() { st<int, int> st1; if(typeid(st1.keytype) == typeid(int)) cout<<"yes"; return 0; } is there way of storing type key , value within structure? st1 structure stores key of type int , value of type int or template type within structure. want later use type comparison. following error.
invalid use of ‘st<int, int>::keytype’ if(typeid(st1.keytype) == typeid(int)) i want store type initialized to stored within structure.
if can use c++11 or newer, can use decltype
if ( typeid(decltype(st1)::keytype) == typeid(int) ) cout<<"yes"; or, better imho, std::is_same (evaluated compile-time)
if ( std::is_same<decltype(st1)::keytype, int>::value ) cout<<"yes"; if can use c++17, last example can written as
if constexpr ( std::is_same<decltype(st1)::keytype, int>::value ) cout<<"yes"; so cout << "yes" compiled or not according value of std::is_same.
wiki
Comments
Post a Comment