Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ template instantiation with identity argument

Tags:

c++

templates

I have ran into yet another problem I do not understand.

The following does not instantiate (argument instantiation fails), why?

template<class E>
void operator[](typename boost::mpl::identity<E>::type e) const;

thank you for your help

like image 714
Anycorn Avatar asked Jan 02 '26 09:01

Anycorn


1 Answers

identity can be used to force you to specify the template argument explicitly. It effectively prevents that function parameter from partaking in template argument deduction.

A qualified type name is one of the non deduced contexts; that is, identity<E>::type will not be used to deduce the template parameter for E.

For example, if you have:

template<class E>
void f(typename boost::mpl::identity<E>::type e) { }

f(42);      // won't work
f<int>(42); // works
like image 81
James McNellis Avatar answered Jan 03 '26 21:01

James McNellis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!