Help: need non-const first_type

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,
 
M

mlimber

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 use Boost.TypeTraits' remove_const
(http://boost.org/doc/html/boost_typetraits/reference.html#boost_typetraits.remove_const)
or Loki's TypeTraits' NonConstType (http://loki-lib.sourceforge.net).

Cheers! --M


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
D

Daniel T.

Paul Dubuc <[email protected]> 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?

I would at least leave open the possibility for arg to be a different
type that is convertible to first_type. That would mean a separate
template parameter, you might want to do the same with val as well.


--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
J

JE

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?
<snip>

MAP::key_type?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
C

Carl Barron

you can use boost or loki stuff or since you want the types T,V
of map<T,V,...> or multimap<T,V,...> they are key_type and mapped_type.

template <class MAP>
int match(const MAP &choices,typename MAP::key_type &arg,
typename MAP::mapped_type &val)
{ /* .... */}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

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,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top