logo
down
shadow

Force evaluation of constexpr static member


Force evaluation of constexpr static member

By : Hitesh Bodar
Date : November 17 2020, 04:28 AM
I wish this help you The simple answer would probably be to simply add another static_assert:
code :
template<size_t ... Sizes>
struct Test
{
    static constexpr size_t Final = check_sizes<Sizes...>();
    static_assert(Final > 0, "");
};
template<size_t ... Sizes>
struct Test
{
    static constexpr decltype(check_sizes<Sizes...>(), size_t()) Final = check_sizes<Sizes...>();
};


Share : facebook icon twitter icon
Why does GCC think that the definition of a constexpr static data member must be marked constexpr?

Why does GCC think that the definition of a constexpr static data member must be marked constexpr?


By : Arab_Aka
Date : March 29 2020, 07:55 AM
this one helps. This looks underspecified to me, I don't see an explicit requirement but we can see why it is an issue from defect report 699: Must constexpr member functions be defined in the class member-specification? which although dealing with constexpr member functions says the following (emphasis mine):
code :
struct T
{
   static constexpr int blah = 1 ;
};

const int T::blah ;
constexpr non-static member function with non-constexpr constructor (gcc,clang differ)

constexpr non-static member function with non-constexpr constructor (gcc,clang differ)


By : Vanthar
Date : March 29 2020, 07:55 AM
this one helps. It's a core defect that was fixed for C++14
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1684
Initializing a static constexpr data member of the base class by using a static constexpr data member of the derived cla

Initializing a static constexpr data member of the base class by using a static constexpr data member of the derived cla


By : JasonYang
Date : March 29 2020, 07:55 AM
I wish this help you For C++14 and 11, Clang is right; however, things have changed in the latest working draft (the future C++17) - see the next section.
The Standard quotes to look for are (from N4140, the draft closest to C++14):
code :
template<class T> struct S 
{
   static constexpr int bar() { return T::foo; }
};

struct U : S<U> { static constexpr int foo = 42; };

int main()
{
   constexpr int b = U::bar();
   static_assert(b == 42, "oops");
}
struct A {
   static constexpr int n = 5; // definition (declaration in C++ 2014)
};

const int A::n; // redundant declaration (definition in C++ 2014)
Defining a static constexpr member by call to constexpr function

Defining a static constexpr member by call to constexpr function


By : sr1ram
Date : March 29 2020, 07:55 AM
seems to work fine Edit:
To deal with floating point values as well as integral types scenarios you could make use of user defined literal template e.g.:
code :
#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
FloatingPointValue<double, (char)50, (char)46, (char)55, (char)49>
#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
Value<char, (char)97, (char)98, (char)99>
Using static constexpr member variables of a non-constexpr object as template arguments

Using static constexpr member variables of a non-constexpr object as template arguments


By : user3375326
Date : March 29 2020, 07:55 AM
this will help I chose a combination of Nathan's answer, and an answer that was deleted, for the implementation of getC.
code :
template <typename T, typename = std::void_t<decltype(std::remove_reference_t<T>::C)>>
constexpr int getC_() { return std::remove_reference_t<T>::C; }

#define getC(e) (getC_<decltype(e)>())
getC(1);  // error: 'C' is not a member of 'std::remove_reference<int>::type' {aka 'int'}
Related Posts Related Posts :
  • C++ Doubly Linked List with Pointers: Object of class isn't constructed properly
  • Using a random string generator in c++ constructor
  • What should I use instead of void as one of the alternative types in an variant?
  • C++ return value from multithreads using reference
  • How to connect multiple TCP IP clients to same server port using c++
  • Defaul compiler generates the reference operator (In C++)?
  • Unable to change directory time stamp after using FILE_FLAG_BACKUP_SEMANTICS
  • vector handling displaying output
  • WSAGetLastError returns WSAENOTSOCK - Cause?
  • C++: How to overload pow for user type?
  • C++ using arrays as multidimensional despite initalising it as 1D with pointer
  • How negate std::is_integral for use in tag dispatch?
  • Retrieve serial number from USB memory (Windows environment c++)
  • g++ error: invalid preprocessing directive #INCLUDE
  • C++ What is the std::for_each() function parameter type?
  • C++: Read individual lines from text file, sort words alphabetically
  • Saving 'this' address into a variable
  • c++ command line arguments in ubuntu terminal
  • Convert "Cartesian coordinates" to "polar coordinates with respect to user specified origin"
  • In what order are local scoped objects destructed?
  • How to use SDL_MapRGB with SDL 2.0
  • how compiler and interpreter work in case of array declaration
  • GSL integration behaves strange
  • Cropping an image with OpenCV and C
  • Find the last line in text file and select the first 10 char and print to a new file?
  • Created many CCSprits but when triggering ccTouchBegan gives the last one allways
  • seekp and seekg don't work with fstream
  • Taking input in Sublime Text 3
  • ld: -bind_at_load and -bitcode_bundle (Xcode setting ENABLE_BITCODE=YES) cannot be used together
  • C++ storing #define as std::string would give size as zero compile time
  • How to use static linking with OpenSSL in C/C++
  • What is the difference between a trap, an error, a failure and program abortion?
  • Dynamic members allocation in qt
  • How to reduce object file size when compiling for VxWorks 5.5.1?
  • Printing char by integer qualifier
  • How to write code to be executed before main() gets control?
  • Blocking socket - waitForReadyRead()
  • std::string related errors and handling them, c++
  • VM interpreter - weighting performance benefits and drawbacks of larger instruction set / dispatch loop
  • C / C++ SHIFT / OFFSET / MOVE a Bitmap to the LEFT or RIGHT?
  • Printing numbers column by column
  • How do you change your app icon in visual studio 2013?
  • Fast Screen Transfer
  • c++ Read text file and input the numbers into a 2D array
  • Why are my C++ pointers suddenly diverging?
  • Is there a macro-based adapter to make a functor from a class?
  • CRTP and multilevel inheritance
  • How to implement timer for each object in c++?
  • Stuck when testing custom list class in c++
  • Using each member of class within one function to calculate average
  • check whether if two squares are intersecting with each other
  • Glm Quaternion lookat function
  • Is there guarantee heap allocated block address will not change(implicitly)?
  • Cosine Calculation without cmath library
  • Manually deleting a pointer returned by function
  • Avoid output interleaving
  • C++ error : Expected an identifier
  • Segmentation fault when I call operator new in linux mint
  • Recursively Solving A Sudoku Puzzle Using Backtracking Theoretically
  • lambda closure type and default argument in function template
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org