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

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

user9733
1#
user9733 Published in September 19, 2018, 3:16 am

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.
share|improve this question
  • 1
    Shouldn't it be template<typename T> using myMap = ... ? – songyuanyao Jan 12 at 7:18
  • @songyuanyao It's still not work, is it work in your computer? – spring cc Jan 12 at 7:24
  • 2
    @springcc Yes. – songyuanyao Jan 12 at 7:27
  • You just can't do it inside the function. Move it out of the function. – songyuanyao Jan 12 at 7:33
  • @songyuanyao Yes, it solved! – spring cc Jan 12 at 7:37

1 Answer 1

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO