Help with non-const first_type in std::map

P

Paul Dubuc

Question: In the following template function signature

template <class MAP>
int matchAbbreviationMap(
const MAP & choices,
typename MAP::value_type::first_type & arg,
typename MAP::value_type::second_type & val)
{
...

MAP is a std::map or std::multi_map so MAP::value_type::first_type is a const
type. I want arg to have the same type but to be non-const. Is there some
way to do this generically without specifying a redundant type in the template
parameters?

Thanks,
 
V

Victor Bazarov

Paul said:
Question: In the following template function signature

template <class MAP>
int matchAbbreviationMap(
const MAP & choices,
typename MAP::value_type::first_type & arg,
typename MAP::value_type::second_type & val)
{
...

MAP is a std::map or std::multi_map so MAP::value_type::first_type is
a const type. I want arg to have the same type but to be non-const. Is
there some way to do this generically without specifying a
redundant type in the template parameters?

You could try this:

// auxiliary templates for type extraction
template<class T> struct drop_const { typedef T type; };
template<class T> struct drop_const<T const &> { typedef T type; }

..
template<class MAP>
int match...
typename drop_const<MAP::value_type::first>::type & arg,
...

Or some such.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top