Retrieve default value of in-class initialized member

Silverlan

Is there any way of directly retrieving the default value of a member, which has been defined using in-class initialization? For example:

struct Test
{
    int someValue = 5;
};

int main(int argc,char *argv[])
{
    auto val = declvalue(Test::someValue); // Something like this; Should return 5
    std::cout<<val<<std::endl;
    for(;;);
    return 0;
}

Basically something that 'copies' (Similar to decltype) the entire declaration, including the default value. Does something like that exist?

krzaq

If your type is default constructible, you can write your own declvalue:

template<typename T, typename C>
constexpr T declvalue(T C::* ptr)
{
    return C{}.*ptr;
}

which would be used as follows:

int main() {
    cout << declvalue(&Test::someValue) << endl;
}

live demo

This particular case seems to optimize well, but I suggest wariness.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is member value in the class initialized when an object is created?

default value for a class member in c++

Java default value of instance variables of a class not initialized to zero for int

When is a default value initialized?

C++11 default constructor behavior with class member default value

how to create a templated class that has a member of that same class?, initialized it's value at some point?, and avoid pointers if possible

Template class specialization with default value for member variables and methods

Typescript: What is the difference between an optional and a not optional class member with default value

LinkedList default value when initialized

Why doesn't the member of the base class keep the value it was first initialized with in the constructor?

How a pointer to an object which is not initialized by an address of object assign value to data member of class?

Class Template Static Member Initialized Twice

Why are my fields initialized to null or to the default value of zero when I've declared and initialized them in my class' constructor?

What if a static member is not initialized and the member type is class itself?

Using a class member as a default argument for a member function

Lock on an class member vs having that object initialized in class' method?

Why aren't union member data default-initialized?

Using a class data-member in a class function as default value, c++

Which is the default value for "cancelable" for initialized event

What is the default value for not initialized variables in C++?

Default value of function parameter initialized by list initialization

Initialize member reference with default value

Member property as default value for method

default value for struct member in C

Could a class type with deleted default constructor be default initialized?

Visual Studio C++ option warn if class member is not initialized

Why F# class member is initialized every time it's called?

Member of nested class isn't being initialized by the constructor

How to declare a zero initialized class member in C++?