Constexpr operator new












2














is it possible to overload the operator new to be constexpr function? Something like:



constexpr void * operator new( std::size_t count );


The reason why would be to execute constexpr function within the overloaded operator body where
count argument value would be an input data... As the operator is invoked by:



SomeClass * foo = new SomeClass(); 


The size of the data type is know at compile time, isn’t it? (count== sizeof(SomeClass)) So the count can be considered as compile time constant?



constexpr void * operator new( std::size_t count )
{
if constexpr ( count >= 10 ) { /* do some compile-time bussiness */ }
}


Many thanks in advance to anyone willing to help!










share|improve this question


















  • 3




    constexpr cannot have side effects, thus this would be contradictory
    – OznOg
    Nov 21 '18 at 19:19






  • 4




    There is no such thing as compile-time dynamic memory allocation. Allocating memory for your program at compile time is by definition just specifying a quantity of static memory necessary at runtime. If you can calculate at compile time you need N bytes at runtime for your task, then just have a properly aligned static buffer of size N.
    – François Andrieux
    Nov 21 '18 at 19:31








  • 4




    It doesn't matter anyway. [expr.const] prohibits new-expressions in constant expressions across the board.
    – T.C.
    Nov 21 '18 at 20:04






  • 5




    You want to look at the C++2a papers trying to make everything constexpr.
    – Marc Glisse
    Nov 21 '18 at 20:10






  • 1




    @FrançoisAndrieux Read this: open-std.org/JTC1/SC22/WG21/docs/papers/2018/p0784r1.html
    – Oliv
    Nov 21 '18 at 21:27
















2














is it possible to overload the operator new to be constexpr function? Something like:



constexpr void * operator new( std::size_t count );


The reason why would be to execute constexpr function within the overloaded operator body where
count argument value would be an input data... As the operator is invoked by:



SomeClass * foo = new SomeClass(); 


The size of the data type is know at compile time, isn’t it? (count== sizeof(SomeClass)) So the count can be considered as compile time constant?



constexpr void * operator new( std::size_t count )
{
if constexpr ( count >= 10 ) { /* do some compile-time bussiness */ }
}


Many thanks in advance to anyone willing to help!










share|improve this question


















  • 3




    constexpr cannot have side effects, thus this would be contradictory
    – OznOg
    Nov 21 '18 at 19:19






  • 4




    There is no such thing as compile-time dynamic memory allocation. Allocating memory for your program at compile time is by definition just specifying a quantity of static memory necessary at runtime. If you can calculate at compile time you need N bytes at runtime for your task, then just have a properly aligned static buffer of size N.
    – François Andrieux
    Nov 21 '18 at 19:31








  • 4




    It doesn't matter anyway. [expr.const] prohibits new-expressions in constant expressions across the board.
    – T.C.
    Nov 21 '18 at 20:04






  • 5




    You want to look at the C++2a papers trying to make everything constexpr.
    – Marc Glisse
    Nov 21 '18 at 20:10






  • 1




    @FrançoisAndrieux Read this: open-std.org/JTC1/SC22/WG21/docs/papers/2018/p0784r1.html
    – Oliv
    Nov 21 '18 at 21:27














2












2








2







is it possible to overload the operator new to be constexpr function? Something like:



constexpr void * operator new( std::size_t count );


The reason why would be to execute constexpr function within the overloaded operator body where
count argument value would be an input data... As the operator is invoked by:



SomeClass * foo = new SomeClass(); 


The size of the data type is know at compile time, isn’t it? (count== sizeof(SomeClass)) So the count can be considered as compile time constant?



constexpr void * operator new( std::size_t count )
{
if constexpr ( count >= 10 ) { /* do some compile-time bussiness */ }
}


Many thanks in advance to anyone willing to help!










share|improve this question













is it possible to overload the operator new to be constexpr function? Something like:



constexpr void * operator new( std::size_t count );


The reason why would be to execute constexpr function within the overloaded operator body where
count argument value would be an input data... As the operator is invoked by:



SomeClass * foo = new SomeClass(); 


The size of the data type is know at compile time, isn’t it? (count== sizeof(SomeClass)) So the count can be considered as compile time constant?



constexpr void * operator new( std::size_t count )
{
if constexpr ( count >= 10 ) { /* do some compile-time bussiness */ }
}


Many thanks in advance to anyone willing to help!







c++ c++17 constexpr if-constexpr






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 19:18









Martin KopeckýMartin Kopecký

1866




1866








  • 3




    constexpr cannot have side effects, thus this would be contradictory
    – OznOg
    Nov 21 '18 at 19:19






  • 4




    There is no such thing as compile-time dynamic memory allocation. Allocating memory for your program at compile time is by definition just specifying a quantity of static memory necessary at runtime. If you can calculate at compile time you need N bytes at runtime for your task, then just have a properly aligned static buffer of size N.
    – François Andrieux
    Nov 21 '18 at 19:31








  • 4




    It doesn't matter anyway. [expr.const] prohibits new-expressions in constant expressions across the board.
    – T.C.
    Nov 21 '18 at 20:04






  • 5




    You want to look at the C++2a papers trying to make everything constexpr.
    – Marc Glisse
    Nov 21 '18 at 20:10






  • 1




    @FrançoisAndrieux Read this: open-std.org/JTC1/SC22/WG21/docs/papers/2018/p0784r1.html
    – Oliv
    Nov 21 '18 at 21:27














  • 3




    constexpr cannot have side effects, thus this would be contradictory
    – OznOg
    Nov 21 '18 at 19:19






  • 4




    There is no such thing as compile-time dynamic memory allocation. Allocating memory for your program at compile time is by definition just specifying a quantity of static memory necessary at runtime. If you can calculate at compile time you need N bytes at runtime for your task, then just have a properly aligned static buffer of size N.
    – François Andrieux
    Nov 21 '18 at 19:31








  • 4




    It doesn't matter anyway. [expr.const] prohibits new-expressions in constant expressions across the board.
    – T.C.
    Nov 21 '18 at 20:04






  • 5




    You want to look at the C++2a papers trying to make everything constexpr.
    – Marc Glisse
    Nov 21 '18 at 20:10






  • 1




    @FrançoisAndrieux Read this: open-std.org/JTC1/SC22/WG21/docs/papers/2018/p0784r1.html
    – Oliv
    Nov 21 '18 at 21:27








3




3




constexpr cannot have side effects, thus this would be contradictory
– OznOg
Nov 21 '18 at 19:19




constexpr cannot have side effects, thus this would be contradictory
– OznOg
Nov 21 '18 at 19:19




4




4




There is no such thing as compile-time dynamic memory allocation. Allocating memory for your program at compile time is by definition just specifying a quantity of static memory necessary at runtime. If you can calculate at compile time you need N bytes at runtime for your task, then just have a properly aligned static buffer of size N.
– François Andrieux
Nov 21 '18 at 19:31






There is no such thing as compile-time dynamic memory allocation. Allocating memory for your program at compile time is by definition just specifying a quantity of static memory necessary at runtime. If you can calculate at compile time you need N bytes at runtime for your task, then just have a properly aligned static buffer of size N.
– François Andrieux
Nov 21 '18 at 19:31






4




4




It doesn't matter anyway. [expr.const] prohibits new-expressions in constant expressions across the board.
– T.C.
Nov 21 '18 at 20:04




It doesn't matter anyway. [expr.const] prohibits new-expressions in constant expressions across the board.
– T.C.
Nov 21 '18 at 20:04




5




5




You want to look at the C++2a papers trying to make everything constexpr.
– Marc Glisse
Nov 21 '18 at 20:10




You want to look at the C++2a papers trying to make everything constexpr.
– Marc Glisse
Nov 21 '18 at 20:10




1




1




@FrançoisAndrieux Read this: open-std.org/JTC1/SC22/WG21/docs/papers/2018/p0784r1.html
– Oliv
Nov 21 '18 at 21:27




@FrançoisAndrieux Read this: open-std.org/JTC1/SC22/WG21/docs/papers/2018/p0784r1.html
– Oliv
Nov 21 '18 at 21:27












1 Answer
1






active

oldest

votes


















4














You can't overload operator new to be constexpr, the main problem is attributed to the C++ standard directive §9.1.5/1 The constexpr specifier [dcl.constexpr] (Emphasis Mine):




The constexpr specifier shall be applied only to the definition of a
variable or variable template or the declaration of a function or
function template. A function or static data member declared with the
constexpr specifier is implicitly an inline function or variable
(9.1.6). If any declaration of a function or function template has a
constexpr specifier, then all its declarations shall contain the
constexpr specifier
.




That is, in order to overload operator new all its previous declarations must also be constexpr, which they aren't and thus overloading it as constexpr you get a compile time error.






share|improve this answer























  • I'm not 100% sure that this is correct. Doesn't this talk about the case when there are several declarations of the same function (with the same signature)? I don't really see the rationale behind, if one overload was constexpr, then all of them must be constexpr as well.
    – geza
    Nov 21 '18 at 20:19










  • Ok, Many thanks you all Guys for your replies.
    – Martin Kopecký
    Nov 21 '18 at 20:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419133%2fconstexpr-operator-new%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














You can't overload operator new to be constexpr, the main problem is attributed to the C++ standard directive §9.1.5/1 The constexpr specifier [dcl.constexpr] (Emphasis Mine):




The constexpr specifier shall be applied only to the definition of a
variable or variable template or the declaration of a function or
function template. A function or static data member declared with the
constexpr specifier is implicitly an inline function or variable
(9.1.6). If any declaration of a function or function template has a
constexpr specifier, then all its declarations shall contain the
constexpr specifier
.




That is, in order to overload operator new all its previous declarations must also be constexpr, which they aren't and thus overloading it as constexpr you get a compile time error.






share|improve this answer























  • I'm not 100% sure that this is correct. Doesn't this talk about the case when there are several declarations of the same function (with the same signature)? I don't really see the rationale behind, if one overload was constexpr, then all of them must be constexpr as well.
    – geza
    Nov 21 '18 at 20:19










  • Ok, Many thanks you all Guys for your replies.
    – Martin Kopecký
    Nov 21 '18 at 20:19
















4














You can't overload operator new to be constexpr, the main problem is attributed to the C++ standard directive §9.1.5/1 The constexpr specifier [dcl.constexpr] (Emphasis Mine):




The constexpr specifier shall be applied only to the definition of a
variable or variable template or the declaration of a function or
function template. A function or static data member declared with the
constexpr specifier is implicitly an inline function or variable
(9.1.6). If any declaration of a function or function template has a
constexpr specifier, then all its declarations shall contain the
constexpr specifier
.




That is, in order to overload operator new all its previous declarations must also be constexpr, which they aren't and thus overloading it as constexpr you get a compile time error.






share|improve this answer























  • I'm not 100% sure that this is correct. Doesn't this talk about the case when there are several declarations of the same function (with the same signature)? I don't really see the rationale behind, if one overload was constexpr, then all of them must be constexpr as well.
    – geza
    Nov 21 '18 at 20:19










  • Ok, Many thanks you all Guys for your replies.
    – Martin Kopecký
    Nov 21 '18 at 20:19














4












4








4






You can't overload operator new to be constexpr, the main problem is attributed to the C++ standard directive §9.1.5/1 The constexpr specifier [dcl.constexpr] (Emphasis Mine):




The constexpr specifier shall be applied only to the definition of a
variable or variable template or the declaration of a function or
function template. A function or static data member declared with the
constexpr specifier is implicitly an inline function or variable
(9.1.6). If any declaration of a function or function template has a
constexpr specifier, then all its declarations shall contain the
constexpr specifier
.




That is, in order to overload operator new all its previous declarations must also be constexpr, which they aren't and thus overloading it as constexpr you get a compile time error.






share|improve this answer














You can't overload operator new to be constexpr, the main problem is attributed to the C++ standard directive §9.1.5/1 The constexpr specifier [dcl.constexpr] (Emphasis Mine):




The constexpr specifier shall be applied only to the definition of a
variable or variable template or the declaration of a function or
function template. A function or static data member declared with the
constexpr specifier is implicitly an inline function or variable
(9.1.6). If any declaration of a function or function template has a
constexpr specifier, then all its declarations shall contain the
constexpr specifier
.




That is, in order to overload operator new all its previous declarations must also be constexpr, which they aren't and thus overloading it as constexpr you get a compile time error.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 20:06

























answered Nov 21 '18 at 19:58









101010101010

31.4k762123




31.4k762123












  • I'm not 100% sure that this is correct. Doesn't this talk about the case when there are several declarations of the same function (with the same signature)? I don't really see the rationale behind, if one overload was constexpr, then all of them must be constexpr as well.
    – geza
    Nov 21 '18 at 20:19










  • Ok, Many thanks you all Guys for your replies.
    – Martin Kopecký
    Nov 21 '18 at 20:19


















  • I'm not 100% sure that this is correct. Doesn't this talk about the case when there are several declarations of the same function (with the same signature)? I don't really see the rationale behind, if one overload was constexpr, then all of them must be constexpr as well.
    – geza
    Nov 21 '18 at 20:19










  • Ok, Many thanks you all Guys for your replies.
    – Martin Kopecký
    Nov 21 '18 at 20:19
















I'm not 100% sure that this is correct. Doesn't this talk about the case when there are several declarations of the same function (with the same signature)? I don't really see the rationale behind, if one overload was constexpr, then all of them must be constexpr as well.
– geza
Nov 21 '18 at 20:19




I'm not 100% sure that this is correct. Doesn't this talk about the case when there are several declarations of the same function (with the same signature)? I don't really see the rationale behind, if one overload was constexpr, then all of them must be constexpr as well.
– geza
Nov 21 '18 at 20:19












Ok, Many thanks you all Guys for your replies.
– Martin Kopecký
Nov 21 '18 at 20:19




Ok, Many thanks you all Guys for your replies.
– Martin Kopecký
Nov 21 '18 at 20:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419133%2fconstexpr-operator-new%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'