What is the purpose of marking the set function (setter) as constexpr? [duplicate]












11
















This question already has an answer here:




  • What is the use of a constexpr on a non-const member function?

    2 answers




I cannot understand the purpose of marking the setter function as constexpr, that is allowed since C++14.
My misunderstanding comes from the next situation:
I declare a class with a constexpr c-tor, and I am about to use it in a constexpr context, by creating a constexpr instance of that class constexpr Point p1. An object p1 now is constant and its value could not be changed, so the constexpr setter could not be called.
On the other hand, when I create an instance of my class Point in a non-constexpr context Point p, I can call the setter for that object, but now setter will not execute at compile-time, because the object is not constexpr!



As a result, I do not understand how can I enhance the performance of my code using constexpr for setters.



This is the code that demonstrates calling a constexpr setter on an non-constexpr object, that means run-time computation, instead of the compile-time:



class Point {
public:
constexpr Point(int a, int b)
: x(a), y(b) {}

constexpr int getX() const noexcept { return x; }
constexpr int getY() const noexcept { return y; }

constexpr void setX(int newX) noexcept { x = newX; }
constexpr void setY(int newY) noexcept { y = newY; }
private:
int x;
int y;
};


int main() {
Point p{4, 2};
constexpr Point p1{4, 2};

p.setX(2);
}


Could anyone help me to understand what is the purpose of marking the setter function as constexpr?










share|improve this question













marked as duplicate by Barry c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 7 at 14:55


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    The constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time. plain and simple. See is as if you had written Point p2{2, 2} and used it instead of p in the subsequent statement.

    – UmNyobe
    Feb 7 at 14:26


















11
















This question already has an answer here:




  • What is the use of a constexpr on a non-const member function?

    2 answers




I cannot understand the purpose of marking the setter function as constexpr, that is allowed since C++14.
My misunderstanding comes from the next situation:
I declare a class with a constexpr c-tor, and I am about to use it in a constexpr context, by creating a constexpr instance of that class constexpr Point p1. An object p1 now is constant and its value could not be changed, so the constexpr setter could not be called.
On the other hand, when I create an instance of my class Point in a non-constexpr context Point p, I can call the setter for that object, but now setter will not execute at compile-time, because the object is not constexpr!



As a result, I do not understand how can I enhance the performance of my code using constexpr for setters.



This is the code that demonstrates calling a constexpr setter on an non-constexpr object, that means run-time computation, instead of the compile-time:



class Point {
public:
constexpr Point(int a, int b)
: x(a), y(b) {}

constexpr int getX() const noexcept { return x; }
constexpr int getY() const noexcept { return y; }

constexpr void setX(int newX) noexcept { x = newX; }
constexpr void setY(int newY) noexcept { y = newY; }
private:
int x;
int y;
};


int main() {
Point p{4, 2};
constexpr Point p1{4, 2};

p.setX(2);
}


Could anyone help me to understand what is the purpose of marking the setter function as constexpr?










share|improve this question













marked as duplicate by Barry c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 7 at 14:55


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    The constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time. plain and simple. See is as if you had written Point p2{2, 2} and used it instead of p in the subsequent statement.

    – UmNyobe
    Feb 7 at 14:26
















11












11








11


1







This question already has an answer here:




  • What is the use of a constexpr on a non-const member function?

    2 answers




I cannot understand the purpose of marking the setter function as constexpr, that is allowed since C++14.
My misunderstanding comes from the next situation:
I declare a class with a constexpr c-tor, and I am about to use it in a constexpr context, by creating a constexpr instance of that class constexpr Point p1. An object p1 now is constant and its value could not be changed, so the constexpr setter could not be called.
On the other hand, when I create an instance of my class Point in a non-constexpr context Point p, I can call the setter for that object, but now setter will not execute at compile-time, because the object is not constexpr!



As a result, I do not understand how can I enhance the performance of my code using constexpr for setters.



This is the code that demonstrates calling a constexpr setter on an non-constexpr object, that means run-time computation, instead of the compile-time:



class Point {
public:
constexpr Point(int a, int b)
: x(a), y(b) {}

constexpr int getX() const noexcept { return x; }
constexpr int getY() const noexcept { return y; }

constexpr void setX(int newX) noexcept { x = newX; }
constexpr void setY(int newY) noexcept { y = newY; }
private:
int x;
int y;
};


int main() {
Point p{4, 2};
constexpr Point p1{4, 2};

p.setX(2);
}


Could anyone help me to understand what is the purpose of marking the setter function as constexpr?










share|improve this question















This question already has an answer here:




  • What is the use of a constexpr on a non-const member function?

    2 answers




I cannot understand the purpose of marking the setter function as constexpr, that is allowed since C++14.
My misunderstanding comes from the next situation:
I declare a class with a constexpr c-tor, and I am about to use it in a constexpr context, by creating a constexpr instance of that class constexpr Point p1. An object p1 now is constant and its value could not be changed, so the constexpr setter could not be called.
On the other hand, when I create an instance of my class Point in a non-constexpr context Point p, I can call the setter for that object, but now setter will not execute at compile-time, because the object is not constexpr!



As a result, I do not understand how can I enhance the performance of my code using constexpr for setters.



This is the code that demonstrates calling a constexpr setter on an non-constexpr object, that means run-time computation, instead of the compile-time:



class Point {
public:
constexpr Point(int a, int b)
: x(a), y(b) {}

constexpr int getX() const noexcept { return x; }
constexpr int getY() const noexcept { return y; }

constexpr void setX(int newX) noexcept { x = newX; }
constexpr void setY(int newY) noexcept { y = newY; }
private:
int x;
int y;
};


int main() {
Point p{4, 2};
constexpr Point p1{4, 2};

p.setX(2);
}


Could anyone help me to understand what is the purpose of marking the setter function as constexpr?





This question already has an answer here:




  • What is the use of a constexpr on a non-const member function?

    2 answers








c++ c++14 constexpr






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 7 at 14:20









mariia_kornievamariia_kornieva

5814




5814




marked as duplicate by Barry c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 7 at 14:55


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Barry c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 7 at 14:55


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    The constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time. plain and simple. See is as if you had written Point p2{2, 2} and used it instead of p in the subsequent statement.

    – UmNyobe
    Feb 7 at 14:26
















  • 1





    The constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time. plain and simple. See is as if you had written Point p2{2, 2} and used it instead of p in the subsequent statement.

    – UmNyobe
    Feb 7 at 14:26










1




1





The constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time. plain and simple. See is as if you had written Point p2{2, 2} and used it instead of p in the subsequent statement.

– UmNyobe
Feb 7 at 14:26







The constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time. plain and simple. See is as if you had written Point p2{2, 2} and used it instead of p in the subsequent statement.

– UmNyobe
Feb 7 at 14:26














3 Answers
3






active

oldest

votes


















15














Basically it is nice when you have to deal with constexpr function.



struct Object {
constexpr void set(int n);
int m_n = 0;
};

constexpr Object function() {
Object a;
a.set(5);
return a;
}

constexpr Object a = function();



The idea is to be able to perform compile time initialization within another functions that will be executed at the compile time. It is not done to be applied on constexpr object.



Another things to know is that constexpr member functions are not const member functions since C++14 :).






share|improve this answer

































    6














    The need arise with new constexpr rule with C++14: inside constexpr function, you can now use multiple statements, including for loops and control flow.



    Here's an example:



    constexpr int count5(int start) {
    int acc = 0;

    for (int i = start ; i<start+5 ; ++i) {
    acc += i;
    }

    return acc;
    }

    constexpr int value = count5(10); // value is 60!


    As you can see, we can do many mutation to variable in a constexpr context. The compiler becomes like an interpreter, and as long as the result of the constexpr fonction is consistent and you don't mutate already computed constexpr variables, it may mutate the values during the interpretation.






    share|improve this answer































      -1














      A function with constexpr qualifier will evaluate the return of the function at compile time, which can significantly boost performance of a program (no extra computation, no instruction counter jumping, etc.). There are a few requirements to qualify a function thusly, so check out this explanation from IBM.






      share|improve this answer






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        15














        Basically it is nice when you have to deal with constexpr function.



        struct Object {
        constexpr void set(int n);
        int m_n = 0;
        };

        constexpr Object function() {
        Object a;
        a.set(5);
        return a;
        }

        constexpr Object a = function();



        The idea is to be able to perform compile time initialization within another functions that will be executed at the compile time. It is not done to be applied on constexpr object.



        Another things to know is that constexpr member functions are not const member functions since C++14 :).






        share|improve this answer






























          15














          Basically it is nice when you have to deal with constexpr function.



          struct Object {
          constexpr void set(int n);
          int m_n = 0;
          };

          constexpr Object function() {
          Object a;
          a.set(5);
          return a;
          }

          constexpr Object a = function();



          The idea is to be able to perform compile time initialization within another functions that will be executed at the compile time. It is not done to be applied on constexpr object.



          Another things to know is that constexpr member functions are not const member functions since C++14 :).






          share|improve this answer




























            15












            15








            15







            Basically it is nice when you have to deal with constexpr function.



            struct Object {
            constexpr void set(int n);
            int m_n = 0;
            };

            constexpr Object function() {
            Object a;
            a.set(5);
            return a;
            }

            constexpr Object a = function();



            The idea is to be able to perform compile time initialization within another functions that will be executed at the compile time. It is not done to be applied on constexpr object.



            Another things to know is that constexpr member functions are not const member functions since C++14 :).






            share|improve this answer















            Basically it is nice when you have to deal with constexpr function.



            struct Object {
            constexpr void set(int n);
            int m_n = 0;
            };

            constexpr Object function() {
            Object a;
            a.set(5);
            return a;
            }

            constexpr Object a = function();



            The idea is to be able to perform compile time initialization within another functions that will be executed at the compile time. It is not done to be applied on constexpr object.



            Another things to know is that constexpr member functions are not const member functions since C++14 :).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 7 at 14:54









            Barry

            182k20318582




            182k20318582










            answered Feb 7 at 14:24









            Antoine MorrierAntoine Morrier

            2,065719




            2,065719

























                6














                The need arise with new constexpr rule with C++14: inside constexpr function, you can now use multiple statements, including for loops and control flow.



                Here's an example:



                constexpr int count5(int start) {
                int acc = 0;

                for (int i = start ; i<start+5 ; ++i) {
                acc += i;
                }

                return acc;
                }

                constexpr int value = count5(10); // value is 60!


                As you can see, we can do many mutation to variable in a constexpr context. The compiler becomes like an interpreter, and as long as the result of the constexpr fonction is consistent and you don't mutate already computed constexpr variables, it may mutate the values during the interpretation.






                share|improve this answer




























                  6














                  The need arise with new constexpr rule with C++14: inside constexpr function, you can now use multiple statements, including for loops and control flow.



                  Here's an example:



                  constexpr int count5(int start) {
                  int acc = 0;

                  for (int i = start ; i<start+5 ; ++i) {
                  acc += i;
                  }

                  return acc;
                  }

                  constexpr int value = count5(10); // value is 60!


                  As you can see, we can do many mutation to variable in a constexpr context. The compiler becomes like an interpreter, and as long as the result of the constexpr fonction is consistent and you don't mutate already computed constexpr variables, it may mutate the values during the interpretation.






                  share|improve this answer


























                    6












                    6








                    6







                    The need arise with new constexpr rule with C++14: inside constexpr function, you can now use multiple statements, including for loops and control flow.



                    Here's an example:



                    constexpr int count5(int start) {
                    int acc = 0;

                    for (int i = start ; i<start+5 ; ++i) {
                    acc += i;
                    }

                    return acc;
                    }

                    constexpr int value = count5(10); // value is 60!


                    As you can see, we can do many mutation to variable in a constexpr context. The compiler becomes like an interpreter, and as long as the result of the constexpr fonction is consistent and you don't mutate already computed constexpr variables, it may mutate the values during the interpretation.






                    share|improve this answer













                    The need arise with new constexpr rule with C++14: inside constexpr function, you can now use multiple statements, including for loops and control flow.



                    Here's an example:



                    constexpr int count5(int start) {
                    int acc = 0;

                    for (int i = start ; i<start+5 ; ++i) {
                    acc += i;
                    }

                    return acc;
                    }

                    constexpr int value = count5(10); // value is 60!


                    As you can see, we can do many mutation to variable in a constexpr context. The compiler becomes like an interpreter, and as long as the result of the constexpr fonction is consistent and you don't mutate already computed constexpr variables, it may mutate the values during the interpretation.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 7 at 14:29









                    Guillaume RacicotGuillaume Racicot

                    14.2k53265




                    14.2k53265























                        -1














                        A function with constexpr qualifier will evaluate the return of the function at compile time, which can significantly boost performance of a program (no extra computation, no instruction counter jumping, etc.). There are a few requirements to qualify a function thusly, so check out this explanation from IBM.






                        share|improve this answer




























                          -1














                          A function with constexpr qualifier will evaluate the return of the function at compile time, which can significantly boost performance of a program (no extra computation, no instruction counter jumping, etc.). There are a few requirements to qualify a function thusly, so check out this explanation from IBM.






                          share|improve this answer


























                            -1












                            -1








                            -1







                            A function with constexpr qualifier will evaluate the return of the function at compile time, which can significantly boost performance of a program (no extra computation, no instruction counter jumping, etc.). There are a few requirements to qualify a function thusly, so check out this explanation from IBM.






                            share|improve this answer













                            A function with constexpr qualifier will evaluate the return of the function at compile time, which can significantly boost performance of a program (no extra computation, no instruction counter jumping, etc.). There are a few requirements to qualify a function thusly, so check out this explanation from IBM.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 7 at 14:28









                            jhill515jhill515

                            133210




                            133210















                                Popular posts from this blog

                                How to make a Squid Proxy server?

                                Is this a new Fibonacci Identity?

                                19世紀