Constexpr operator new
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 business */ }
}
Many thanks in advance to anyone willing to help!
c++ c++17 constexpr if-constexpr
|
show 6 more comments
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 business */ }
}
Many thanks in advance to anyone willing to help!
c++ c++17 constexpr if-constexpr
4
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 needN
bytes at runtime for your task, then just have a properly alignedstatic
buffer of sizeN
.
– François Andrieux
Nov 21 '18 at 19:31
5
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
|
show 6 more comments
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 business */ }
}
Many thanks in advance to anyone willing to help!
c++ c++17 constexpr if-constexpr
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 business */ }
}
Many thanks in advance to anyone willing to help!
c++ c++17 constexpr if-constexpr
c++ c++17 constexpr if-constexpr
edited Jan 26 at 15:17
marc_s
577k12911151260
577k12911151260
asked Nov 21 '18 at 19:18
Martin KopeckýMartin Kopecký
1866
1866
4
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 needN
bytes at runtime for your task, then just have a properly alignedstatic
buffer of sizeN
.
– François Andrieux
Nov 21 '18 at 19:31
5
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
|
show 6 more comments
4
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 needN
bytes at runtime for your task, then just have a properly alignedstatic
buffer of sizeN
.
– François Andrieux
Nov 21 '18 at 19:31
5
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
4
4
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
5
5
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
|
show 6 more comments
1 Answer
1
active
oldest
votes
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.
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 wasconstexpr
, then all of them must beconstexpr
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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 wasconstexpr
, then all of them must beconstexpr
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
add a comment |
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.
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 wasconstexpr
, then all of them must beconstexpr
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
add a comment |
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.
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.
edited Nov 21 '18 at 20:06
answered Nov 21 '18 at 19:58
101010101010
31.6k863124
31.6k863124
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 wasconstexpr
, then all of them must beconstexpr
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
add a comment |
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 wasconstexpr
, then all of them must beconstexpr
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
4
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 alignedstatic
buffer of sizeN
.– François Andrieux
Nov 21 '18 at 19:31
5
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