Defining a constexpr static member by calling the constexpr function

My problem is the following. I want to sort a list of types based on a list of constexpr values. The problem can be reduced to this function:

template <typename U, typename V>
auto min(U,V) -> std::conditional_t<U::value < V::value, U, V>
{ return {}; }

whereas the value must be some kind of static constexpr element of each type, respectively. The following snippet demonstrates usage:

// (I)

// This must even be declared outside of a function body due to the statics :(
struct X { static constexpr double value = 2.; };
struct Y { static constexpr double value = 1.; };

int main()
{
    X x;
    Y y;
    auto z = min(x,y);
    std::cout << typeid(z).name() << " : " << z.value << std::endl;
}

My goal is to provide value when I call the function. The closest that I have achieved this goal is the following

template <double (*F)()>
struct Value { static constexpr double value = F(); };

which can be called like this using lambdas:

// (II)
auto w = min(Value<[]{ return 3.14; }>{}, Value<[]{ return 2.71; }>{});
std::cout << typeid(w).name() << " : " << w.value << std::endl;

The actual type to be sorted may be an optional parameter.

The problem is that the above is not valid C ++ in accordance with the standard. However, the last clang compiles this gracefully.

, : ( (II)), , , constexor, inplace (- ) ?


P.S.: std::integral_constant. , , . , constexpr, , .

+4
1

Edit:

, , , , :

#include <type_traits>
#include <utility>
#include <typeinfo>
#include <iostream>

template <class FloatingPointType, class... Cs>
constexpr FloatingPointType char_list_to_(Cs... cs) {
    char arr[] = {cs...};
    FloatingPointType lhs = 0;
    bool contains_dot = false;
    for (std::size_t i = 0; i < sizeof...(Cs) && !(contains_dot |= (arr[i] == '.')); i++) { 
        lhs *= 10;
        lhs += arr[i] - '0';
    }
    FloatingPointType rhs = 0;
    for (int i = sizeof...(Cs) - 1; i > 0 && arr[i] != '.'; i--) {
       rhs /= 10;
       rhs += arr[i] - '0';
    }
    rhs /= 10;
    return (contains_dot)?lhs+rhs:lhs;
}

template <class FloatingPointType, char... Cs>
struct FloatingPointValue {

    static constexpr FloatingPointType value = char_list_to_<FloatingPointType>(Cs...);

    constexpr operator FloatingPointType() {
        return value;
    }
};

template <class FloatingPointType, char... Cs>
constexpr FloatingPointType FloatingPointValue<FloatingPointType, Cs...>::value;

template <char... Cs>
FloatingPointValue<double, Cs...> operator""_fv() {
    return {};
}


template <typename U, typename V>
auto min(U,V) -> std::conditional_t<(U{}<V{}), U, V>
{ return {}; }

int main() {
   auto w = min(3.14_fv, 2.71_fv);
   std::cout << typeid(w).name() << " : " << w.value << std::endl;
}

:

18FloatingPointValueIdJLc50ELc46ELc55ELc49EEE : 2.71

c++filt -t 18FloatingPointValueIdJLc50ELc46ELc55ELc49EEE:

FloatingPointValue<double, (char)50, (char)46, (char)55, (char)49>

[live demo]


, , ++. gnu, clang gcc, :

#include <type_traits>
#include <utility>
#include <typeinfo>
#include <iostream>

template <class CharT, CharT... Cs>
struct Value {

    static constexpr std::size_t size = sizeof...(Cs);
    static constexpr CharT const value[sizeof...(Cs) + 1] = {Cs..., '\0'};

    template <class RHS>
    constexpr bool operator<(RHS) {
        for (std::size_t i = 0; i < size && i < RHS::size; i++) {
            if (value[i] != RHS::value[i]) {
                return value[i] < RHS::value[i];
            }
        }
        return size < RHS::size;
    }
};

template <class CharT, CharT... Cs>
constexpr CharT const Value<CharT, Cs...>::value[sizeof...(Cs) + 1];

template <class CharT, CharT... Cs>
Value<CharT, Cs...> operator""_v() {
    return {};
}


template <typename U, typename V>
auto min(U,V) -> std::conditional_t<(U{}<V{}), U, V>
{ return {}; }

int main() {
   auto w = min("cde"_v, "abc"_v);
   std::cout << typeid(w).name() << " : " << w.value << std::endl;
}

:

5ValueIcJLc97ELc98ELc99EEE : abc

c++filt -t 5ValueIcJLc97ELc98ELc99EEE:

Value<char, (char)97, (char)98, (char)99>

[live demo]

+2

Source: https://habr.com/ru/post/1680193/


All Articles