Anycorn
Anycorn

Reputation: 51555

C++ template instantiation with identity argument

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

Upvotes: 3

Views: 1018

Answers (2)

James McNellis
James McNellis

Reputation: 355297

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

Upvotes: 10

Blair Holloway
Blair Holloway

Reputation: 16519

That seems awfully redundant. identity<E>::type is guaranteed to be equivalent to E, so why not just declare your template as:

template<class E>
void operator[](E e) const;

Upvotes: 0

Related Questions