where _1 and _2 are boost placeholders. The first typedef works just fine; the second returns
no conversion from 'boost::mpl::_2' to 'boost::mpl::_1
So, the second is trying to evaluate std::common_type before the "call" to apply, whereas the first is postponing such evaluation even though it inherits from std::common_type.
We can ignore apply's definition because the issue here is why std::common_type<_1,_2> is instantiated before apply is called, whereas common_type_metafunction<_1,_2> is evaluated later.
For clarity though, here is the definition of apply:
// In the general case, F is either a metafunction class or a placeholder expression
// We will deal with placeholder expressions in partially specialized templates of this one
// In this particular case, we assume F is a metafunction class (i.e. it contains a nested
// metafunction called apply - a metafunction is a template or class if it does not have
// non-type parameters and if it also returns a type called type -- i.e. nested)
template <typename F>
struct lambda
{
typedef F type;
template< typename... Args>
struct apply
{
typedeftypename F::template apply<Args...>::type type;
};
};
template<template<typename> class F>
struct lambda<F<_1>>
{
typedef lambda<F<_1>> type;
template<typename... Args>
struct apply
{
typedeftypename F<Args...>::type type;
};
};
template<template<typename, typename> class F>
struct lambda<F<_1,_2>>
{
typedef lambda<F<_1,_2>> type;
template<typename... Args>
struct apply
{
typedeftypename F<Args...>::type type;
};
};
Why is the instantiation of the superclass of common_type_metafunction not done before the call to apply?
In apply<common_type_metafunction<_1,_2>, float, long>, std::common_type<T1,T2> is a dependent base class; it is not part of the current instantiation.
Thanks JLBorges! My confusion was that when creating an object of a subclass, the superclasses are constructed first, so I thought the same would happen when using class templates .... I am still a bit confused on this topic, if you care to elaborate a bit: why is the superclass not constructed (read: instantiated) before the subclass?
> why is the superclass not constructed (read: instantiated) before the subclass?
Objects are constructed, not classes. The instantiation in this context refers to the instantiation of the template; not the construction of an object. To make the distinction clear: function templates are instatiated.
Templates are subject to two-phase name look up. The first phase is when the template definition is seen, before any template instantiation takes place; the second phase is when an actual instantiation of the template takes place. Names that are deduced to refer to the current instantiation of a class template are looked up during phase one; names that are not part of the current instantiation are looked up during phase two.
For a simplified explanation see: 'Why am I getting errors when my template-derived-class uses a nested type it inherits from its template-base-class?' here: https://isocpp.org/wiki/faq/templates