Any reason to declare constexpr for a function that returns void?

Here is what I read: using an alias for static member functions?

In the answer, I see an offer to use constexpr. What is the reason for using constexprfor functions void.

Please demonstrate a simple use case. I am new to constexpr, so I will not understand the essence of complex examples.

+4
source share
4 answers

, void constexpr, . , , , constexpr , . , , . . , -

class A
{
public:
    constexpr X doSomething(Y arg1) {
        checkInvariant();
        constraintOnYArgument(arg1);
        // ...
        checkInvariant();        
    }

    constexpr X doSomethingElse(Y arg1) {
        checkInvariant();
        constraintOnYArgument(arg1);
        // ...
        checkInvariant();        
    }

private:
    constexpr void constraintOnYArguments(Y arg) {
    }

    constexpr void checkInvariant() {
        // some checks
        if (!some condition) {
            throw std::logic_error("Oh no!");
        }
    }
};
+4

++ 14 void

- , :

- void;

- ;

- ;

- ;

- ( 9), : ,

- (8.5.1) constexpr ,

- .

:

, , , assert-like . void , constexpr , , void.

+2

, constexpr ( ++ 11). , . , : . .

0

. GNU (g++, ld):

constexpr, . :

/* Define output sections */
SECTIONS
{
GPIO    0x48000000 (NOLOAD) : { *(.GPIO) }

.GPIO 0x48000400. POD, . POD gpio_t : mode. constexpr. , , . . , , .

struct gpio_t {
    volatile std::uint32_t mode;
   };

__attribute__ ((section (".GPIO"))) gpio_t Gpio = {0};

static constexpr gpio_t *port {&Gpio};

static constexpr void init () {
    port->mode = 42u;
   };

void main {
    init ();
};

Note: constexprC's idiom in C style does not work, because it is reinterpret_cast<>not suitable for creating pointers constexpr(see C ++ 14 is invalid ). The following errors :

constexpr gpio_t *port {(gpio_t *) 0x48000400};
0
source

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


All Articles