HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

metafunction to determinate the biggest of all supplied types

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
theallsuppliedbiggestdeterminatetypesmetafunction

Problem

Can the metafunction below be implemented more elegantly, with less code? What I have now is not very elegant or smartly written.

#include 

namespace detail
{

template 
struct max_type
{
  typedef typename max_type sizeof(T)), A, T>::type,
    B...
  >::type type;
};

template 
struct max_type
{
  typedef typename std::conditional sizeof(T)), A, T>::type type;
};

}

template 
struct max_type
{
  typedef typename detail::max_type::type type;
};

Solution

How about this? It’s not all that much shorter, but IMHO quite a bit simpler:

#include 

template 
struct max_type
{
    typedef typename max_type::type>::type type;
};

template 
struct max_type
{
    typedef A type;
};

template 
struct max_type
{
    typedef typename std::conditional sizeof(B)), A, B>::type type;
};

Code Snippets

#include <type_traits>

template <typename A, typename... B>
struct max_type
{
    typedef typename max_type<A, typename max_type<B...>::type>::type type;
};

template <typename A>
struct max_type<A>
{
    typedef A type;
};

template <typename A, typename B>
struct max_type<A,B>
{
    typedef typename std::conditional<(sizeof(A) > sizeof(B)), A, B>::type type;
};

Context

StackExchange Code Review Q#28892, answer score: 3

Revisions (0)

No revisions yet.