Calculation of template argument at compile time

I am trying to deduce the larger of two template arguments at compile time. Both template arguments are of type size_t.

I have a template type SomeType that takes a size_t argument as a template. Then I have a function that takes two SomeType parameters with different template_t sizes and I want the return type to be SomeType and its templated size_t to be the larger of the two input size_t sizes.

template <size_t d> struct SomeType {...}

template<size_t d1, size_t d2>
SomeType<the_larger_of_d1_and_d2> Func(SomeType<d1> A, SomeType<d2> B)
{
    ...
}

Is it possible?

+3
source share
4 answers

You can calculate the type directly, without SFINAE:

template<size_t d1, size_t d2>
SomeType<(d1 > d2 ? d1 : d2)> Func(SomeType<d1> A, SomeType<d2> B)
{
}
+3
source

@KonradRudolph , . , , Boost.MPL, . . ,

#include <iostream>
#include <boost/mpl/int.hpp>
#include <boost/mpl/max.hpp>

template<size_t d> 
struct SomeType
: 
    boost::mpl::int_<d> 
{};

template<size_t d1, size_t d2>
typename boost::mpl::max<SomeType<d1>, SomeType<d2> >::type
Func(SomeType<d1> A, SomeType<d2> B) 
{
    return typename boost::mpl::max<SomeType<d1>, SomeType<d2> >::type();
}

int main()
{
    SomeType<2> st2;
    SomeType<3> st3;
    boost::mpl::max<SomeType<2>, SomeType<3> >::type res = Func(st2, st3);
    std::cout << res.value;
}  

Live.

:

  • SomeType boost::mpl::int_ type value, . Boost.MPL
  • boost::mpl::max . , - , .
  • Boost.MPL, .
+1

, SFINAE:

template<size_t one, size_t two>
struct larger {
    static constexpr typename std::enable_if<(one > two), size_t>::type value() {
        return one;
    }

    static constexpr typename std::enable_if<(two >= one, size_t>::type value() {
        return two;
    }
};

template<size_t d1, size_t d2>
SomeType<larger<d1, d2>::value()> Func(SomeType<d1> A, SomeType<d2> B)
{
    ...
}
0

( ) , GIT gist , ( , ) "" (), ( "" ++ 03):

:

template<typename FalseType, typename TrueType, bool condition>
struct ConditionalTypeSelector {
    typedef void ResultType;
};

:

template<typename FalseType, typename TrueType>
struct ConditionalTypeSelector<FalseType,TrueType,false> {
    typedef FalseType ResultType;
};

template<typename FalseType, typename TrueType>
struct ConditionalTypeSelector<FalseType,TrueType,true> {
    typedef TrueType ResultType;
};

:

struct A {
    unsigned char member;
};

struct B {
    int member;
};

struct C {
    long long member;
};

:

#include <iostream>
#include <typeinfo>

using namespace std;

int main() {
    cout << typeid
                ( ConditionalTypeSelector
                      < A,B,(sizeof(A) > sizeof(B))>::ResultType
                ).name() << endl;
    cout << typeid
                ( ConditionalTypeSelector
                      <A,B,(sizeof(B) > sizeof(A)) >::ResultType
                ).name() << endl;
    cout << typeid
                ( ConditionalTypeSelector
                      < A,C,(sizeof(A) > sizeof(C))>::ResultType
                ).name() << endl;
    cout << typeid
                ( ConditionalTypeSelector
                      < C,B,true>::ResultType
                ).name() << endl;
    cout << typeid
                ( ConditionalTypeSelector
                      < C,A,false>::ResultType
                ).name() << endl;

    return 0;
}

template , . enum , , .

0
source

All Articles