Create a class with a constexpr constructor. - C++ Data Type

C++ examples for Data Type:constexpr

Description

Create a class with a constexpr constructor.

Demo Code

#include <array>
#include <cstdint>
#include <iostream>

class MyClass//  w ww  .j  a v  a2  s  .com
{
private:
    int intValue;

public:
    constexpr MyClass(int parameter)
        : intValue{parameter}
    {
    }

    constexpr int GetValue() const
    {
        return intValue;
    }
};

int main()
{
    constexpr int ARRAY_SIZE{ MyClass{ 5 }.GetValue() };
    std::array<int, ARRAY_SIZE> myArray{ 1, 2, 3, 4, 5 };

    for (auto&& number : myArray)
    {
        std::cout << number << std::endl;
    }

    return 0;
}

Result


Related Tutorials