Home how to make this template typedef valid in c++11?
Reply: 1

how to make this template typedef valid in c++11?

spring cc
1#
spring cc Published in 2018-01-12 07:16:54Z

I have a code clip like this

  map<string, map<string, map<string, float> > >  map_f;
  map<string, map<string, map<string, string> > > map_s;
  map<string, map<string, map<string, double> > > map_d;

And I just want to make this code concisely as this:

myMap<float>   map_f;
myMap<string>  map_s;
myMap<double>  map_d;

So I tried to use template and using to achieve this:

template<type T>
    using myMap = map<string, map<string, map<string, T> > >;

However, I got an error as:

error: expected expression
template<type T>

My question is, how to modify this code and what does this error means?

The code has been shown blow:

#include <string>
#include <map>
using namespace std;

int main()
{
  template<typename T>  
    using myMap = map<string, map<string, map<string, T> > >;
  return 0;
} 

I compile it with on c++ 4.2.1 @ mac osX 10.13.2:

g++ -std=c++11 temp_def_cls.cxx -o main

And I got the error:

temp_def_cls.cxx:7:3: error: expected expression
  template<typename T> 
  ^
1 error generated.
iBug
2#
iBug Reply to 2018-01-12 07:34:27Z
template<typename T>
    using myMap = map<string, map<string, map<string, T> > >;

type is nothing special in C++. You need the keyword typename (or class here).

Move that directive out of main(). I used GCC 7.2.0 and the message is much clearer:

t.cpp: In function ‘int main()’:
t.cpp:7:3: error: a template declaration cannot appear at block scope
   template<typename T>
   ^
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.32999 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO