Template ignores [[nodiscard]] attribute
When applied to a function, the [[nodiscard]]
attribute encourages the compiler to issue a warning if it is used in a discarded expression other than a cast to void
. Example:
[[nodiscard]] int callable_return_not_discardable(int n)
{ return n; }
int main()
{
callable_return_not_discardable(0); // warning/error:
// ignoring return value of 'int callable_return_not_discardable(int)',
// declared with attribute nodiscard [-Wunused-result]
(void) callable_return_not_discardable(0); // OK
}
Live demo on gcc-8 and clang-7.
This is nice and useful, until an additional indirection layer is added: templates:
template<class Callable>
void invoke_with_answer(Callable&& callable)
{ callable(42); }
[[nodiscard]] int callable_return_not_discardable(int n)
{ return n; }
int main()
{
invoke_with_answer(callable_return_not_discardable); // OK
}
Live demo on gcc-8 and clang-7.
My question is then:
Is it a missing feature, a consequence of what templates are or should clang and gcc be fixed to issue a warning here?
c++ gcc clang language-lawyer c++17
add a comment |
When applied to a function, the [[nodiscard]]
attribute encourages the compiler to issue a warning if it is used in a discarded expression other than a cast to void
. Example:
[[nodiscard]] int callable_return_not_discardable(int n)
{ return n; }
int main()
{
callable_return_not_discardable(0); // warning/error:
// ignoring return value of 'int callable_return_not_discardable(int)',
// declared with attribute nodiscard [-Wunused-result]
(void) callable_return_not_discardable(0); // OK
}
Live demo on gcc-8 and clang-7.
This is nice and useful, until an additional indirection layer is added: templates:
template<class Callable>
void invoke_with_answer(Callable&& callable)
{ callable(42); }
[[nodiscard]] int callable_return_not_discardable(int n)
{ return n; }
int main()
{
invoke_with_answer(callable_return_not_discardable); // OK
}
Live demo on gcc-8 and clang-7.
My question is then:
Is it a missing feature, a consequence of what templates are or should clang and gcc be fixed to issue a warning here?
c++ gcc clang language-lawyer c++17
It has nothing to do with this being a template. If you replace your first line withusing Callable = int(int);
, you obtain exactly the same behavior. The behavior is a consequence (cf. StoryTellers answer) of what function pointers are.
– Handy999
Nov 21 '18 at 7:17
@Handy Hi. Replying in the comment section is generally frowned upon. If you think you can improve an answer, suggest an edit or answer yourself.
– YSC
Nov 21 '18 at 19:31
add a comment |
When applied to a function, the [[nodiscard]]
attribute encourages the compiler to issue a warning if it is used in a discarded expression other than a cast to void
. Example:
[[nodiscard]] int callable_return_not_discardable(int n)
{ return n; }
int main()
{
callable_return_not_discardable(0); // warning/error:
// ignoring return value of 'int callable_return_not_discardable(int)',
// declared with attribute nodiscard [-Wunused-result]
(void) callable_return_not_discardable(0); // OK
}
Live demo on gcc-8 and clang-7.
This is nice and useful, until an additional indirection layer is added: templates:
template<class Callable>
void invoke_with_answer(Callable&& callable)
{ callable(42); }
[[nodiscard]] int callable_return_not_discardable(int n)
{ return n; }
int main()
{
invoke_with_answer(callable_return_not_discardable); // OK
}
Live demo on gcc-8 and clang-7.
My question is then:
Is it a missing feature, a consequence of what templates are or should clang and gcc be fixed to issue a warning here?
c++ gcc clang language-lawyer c++17
When applied to a function, the [[nodiscard]]
attribute encourages the compiler to issue a warning if it is used in a discarded expression other than a cast to void
. Example:
[[nodiscard]] int callable_return_not_discardable(int n)
{ return n; }
int main()
{
callable_return_not_discardable(0); // warning/error:
// ignoring return value of 'int callable_return_not_discardable(int)',
// declared with attribute nodiscard [-Wunused-result]
(void) callable_return_not_discardable(0); // OK
}
Live demo on gcc-8 and clang-7.
This is nice and useful, until an additional indirection layer is added: templates:
template<class Callable>
void invoke_with_answer(Callable&& callable)
{ callable(42); }
[[nodiscard]] int callable_return_not_discardable(int n)
{ return n; }
int main()
{
invoke_with_answer(callable_return_not_discardable); // OK
}
Live demo on gcc-8 and clang-7.
My question is then:
Is it a missing feature, a consequence of what templates are or should clang and gcc be fixed to issue a warning here?
c++ gcc clang language-lawyer c++17
c++ gcc clang language-lawyer c++17
asked Nov 20 '18 at 11:14
YSCYSC
23.7k553108
23.7k553108
It has nothing to do with this being a template. If you replace your first line withusing Callable = int(int);
, you obtain exactly the same behavior. The behavior is a consequence (cf. StoryTellers answer) of what function pointers are.
– Handy999
Nov 21 '18 at 7:17
@Handy Hi. Replying in the comment section is generally frowned upon. If you think you can improve an answer, suggest an edit or answer yourself.
– YSC
Nov 21 '18 at 19:31
add a comment |
It has nothing to do with this being a template. If you replace your first line withusing Callable = int(int);
, you obtain exactly the same behavior. The behavior is a consequence (cf. StoryTellers answer) of what function pointers are.
– Handy999
Nov 21 '18 at 7:17
@Handy Hi. Replying in the comment section is generally frowned upon. If you think you can improve an answer, suggest an edit or answer yourself.
– YSC
Nov 21 '18 at 19:31
It has nothing to do with this being a template. If you replace your first line with
using Callable = int(int);
, you obtain exactly the same behavior. The behavior is a consequence (cf. StoryTellers answer) of what function pointers are.– Handy999
Nov 21 '18 at 7:17
It has nothing to do with this being a template. If you replace your first line with
using Callable = int(int);
, you obtain exactly the same behavior. The behavior is a consequence (cf. StoryTellers answer) of what function pointers are.– Handy999
Nov 21 '18 at 7:17
@Handy Hi. Replying in the comment section is generally frowned upon. If you think you can improve an answer, suggest an edit or answer yourself.
– YSC
Nov 21 '18 at 19:31
@Handy Hi. Replying in the comment section is generally frowned upon. If you think you can improve an answer, suggest an edit or answer yourself.
– YSC
Nov 21 '18 at 19:31
add a comment |
2 Answers
2
active
oldest
votes
[[nodiscard]]
is not part of a function's signature or type, and not at all preserved when said function is converted to a pointer or bound to a reference. Which is exactly what your example does.
The template, for all intents and purposes cannot "see" the attribute.
@Caleth - Okay, edited to be more percise
– StoryTeller
Nov 20 '18 at 11:34
1
@You beat me to the punch, so I might as well nitpick the things I was verifying :)
– Caleth
Nov 20 '18 at 11:37
Hmm, the standard has stuff like "attributes appertain to the function type". Doesn't this mean that the attribute information is not lost?
– Rakete1111
Nov 21 '18 at 4:40
@Rakete1111 - An attribute has to be applicable to a function's type for that to hold. And[[nodiscard]]
simply isn't. It might be just a oversight.
– StoryTeller
Nov 21 '18 at 4:42
@StoryTeller oh hey, you're right. Thanks :)
– Rakete1111
Nov 21 '18 at 4:43
add a comment |
As explained by StorryTeller, [[nodiscard]]
is not part of a function's signature or type, this is why that information is lost in the context of the template body.
A solution to get that warning propagated would be to add the [[nodiscard]]
attribute to the return type of that function:
template<class Callable>
void invoke_with_answer(Callable&& callable)
{ callable(42); } // warning
struct [[nodiscard]] Int { int value; };
Int callable_return_not_discardable(int n)
{ return {n}; }
int main()
{
invoke_with_answer(callable_return_not_discardable); // note
}
Live demo on gcc-8
enum [[nodiscard]] Int : int{};
is also an option for integral types. At least you get conversions to anint
without too much fuss.
– StoryTeller
Nov 20 '18 at 12:35
1
@StoryTeller That's true :) I'll let it as-is since it's not the point, but true ^^.
– YSC
Nov 20 '18 at 12:36
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%2f53391788%2ftemplate-ignores-nodiscard-attribute%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
[[nodiscard]]
is not part of a function's signature or type, and not at all preserved when said function is converted to a pointer or bound to a reference. Which is exactly what your example does.
The template, for all intents and purposes cannot "see" the attribute.
@Caleth - Okay, edited to be more percise
– StoryTeller
Nov 20 '18 at 11:34
1
@You beat me to the punch, so I might as well nitpick the things I was verifying :)
– Caleth
Nov 20 '18 at 11:37
Hmm, the standard has stuff like "attributes appertain to the function type". Doesn't this mean that the attribute information is not lost?
– Rakete1111
Nov 21 '18 at 4:40
@Rakete1111 - An attribute has to be applicable to a function's type for that to hold. And[[nodiscard]]
simply isn't. It might be just a oversight.
– StoryTeller
Nov 21 '18 at 4:42
@StoryTeller oh hey, you're right. Thanks :)
– Rakete1111
Nov 21 '18 at 4:43
add a comment |
[[nodiscard]]
is not part of a function's signature or type, and not at all preserved when said function is converted to a pointer or bound to a reference. Which is exactly what your example does.
The template, for all intents and purposes cannot "see" the attribute.
@Caleth - Okay, edited to be more percise
– StoryTeller
Nov 20 '18 at 11:34
1
@You beat me to the punch, so I might as well nitpick the things I was verifying :)
– Caleth
Nov 20 '18 at 11:37
Hmm, the standard has stuff like "attributes appertain to the function type". Doesn't this mean that the attribute information is not lost?
– Rakete1111
Nov 21 '18 at 4:40
@Rakete1111 - An attribute has to be applicable to a function's type for that to hold. And[[nodiscard]]
simply isn't. It might be just a oversight.
– StoryTeller
Nov 21 '18 at 4:42
@StoryTeller oh hey, you're right. Thanks :)
– Rakete1111
Nov 21 '18 at 4:43
add a comment |
[[nodiscard]]
is not part of a function's signature or type, and not at all preserved when said function is converted to a pointer or bound to a reference. Which is exactly what your example does.
The template, for all intents and purposes cannot "see" the attribute.
[[nodiscard]]
is not part of a function's signature or type, and not at all preserved when said function is converted to a pointer or bound to a reference. Which is exactly what your example does.
The template, for all intents and purposes cannot "see" the attribute.
edited Nov 20 '18 at 11:33
answered Nov 20 '18 at 11:23
StoryTellerStoryTeller
98.8k12201270
98.8k12201270
@Caleth - Okay, edited to be more percise
– StoryTeller
Nov 20 '18 at 11:34
1
@You beat me to the punch, so I might as well nitpick the things I was verifying :)
– Caleth
Nov 20 '18 at 11:37
Hmm, the standard has stuff like "attributes appertain to the function type". Doesn't this mean that the attribute information is not lost?
– Rakete1111
Nov 21 '18 at 4:40
@Rakete1111 - An attribute has to be applicable to a function's type for that to hold. And[[nodiscard]]
simply isn't. It might be just a oversight.
– StoryTeller
Nov 21 '18 at 4:42
@StoryTeller oh hey, you're right. Thanks :)
– Rakete1111
Nov 21 '18 at 4:43
add a comment |
@Caleth - Okay, edited to be more percise
– StoryTeller
Nov 20 '18 at 11:34
1
@You beat me to the punch, so I might as well nitpick the things I was verifying :)
– Caleth
Nov 20 '18 at 11:37
Hmm, the standard has stuff like "attributes appertain to the function type". Doesn't this mean that the attribute information is not lost?
– Rakete1111
Nov 21 '18 at 4:40
@Rakete1111 - An attribute has to be applicable to a function's type for that to hold. And[[nodiscard]]
simply isn't. It might be just a oversight.
– StoryTeller
Nov 21 '18 at 4:42
@StoryTeller oh hey, you're right. Thanks :)
– Rakete1111
Nov 21 '18 at 4:43
@Caleth - Okay, edited to be more percise
– StoryTeller
Nov 20 '18 at 11:34
@Caleth - Okay, edited to be more percise
– StoryTeller
Nov 20 '18 at 11:34
1
1
@You beat me to the punch, so I might as well nitpick the things I was verifying :)
– Caleth
Nov 20 '18 at 11:37
@You beat me to the punch, so I might as well nitpick the things I was verifying :)
– Caleth
Nov 20 '18 at 11:37
Hmm, the standard has stuff like "attributes appertain to the function type". Doesn't this mean that the attribute information is not lost?
– Rakete1111
Nov 21 '18 at 4:40
Hmm, the standard has stuff like "attributes appertain to the function type". Doesn't this mean that the attribute information is not lost?
– Rakete1111
Nov 21 '18 at 4:40
@Rakete1111 - An attribute has to be applicable to a function's type for that to hold. And
[[nodiscard]]
simply isn't. It might be just a oversight.– StoryTeller
Nov 21 '18 at 4:42
@Rakete1111 - An attribute has to be applicable to a function's type for that to hold. And
[[nodiscard]]
simply isn't. It might be just a oversight.– StoryTeller
Nov 21 '18 at 4:42
@StoryTeller oh hey, you're right. Thanks :)
– Rakete1111
Nov 21 '18 at 4:43
@StoryTeller oh hey, you're right. Thanks :)
– Rakete1111
Nov 21 '18 at 4:43
add a comment |
As explained by StorryTeller, [[nodiscard]]
is not part of a function's signature or type, this is why that information is lost in the context of the template body.
A solution to get that warning propagated would be to add the [[nodiscard]]
attribute to the return type of that function:
template<class Callable>
void invoke_with_answer(Callable&& callable)
{ callable(42); } // warning
struct [[nodiscard]] Int { int value; };
Int callable_return_not_discardable(int n)
{ return {n}; }
int main()
{
invoke_with_answer(callable_return_not_discardable); // note
}
Live demo on gcc-8
enum [[nodiscard]] Int : int{};
is also an option for integral types. At least you get conversions to anint
without too much fuss.
– StoryTeller
Nov 20 '18 at 12:35
1
@StoryTeller That's true :) I'll let it as-is since it's not the point, but true ^^.
– YSC
Nov 20 '18 at 12:36
add a comment |
As explained by StorryTeller, [[nodiscard]]
is not part of a function's signature or type, this is why that information is lost in the context of the template body.
A solution to get that warning propagated would be to add the [[nodiscard]]
attribute to the return type of that function:
template<class Callable>
void invoke_with_answer(Callable&& callable)
{ callable(42); } // warning
struct [[nodiscard]] Int { int value; };
Int callable_return_not_discardable(int n)
{ return {n}; }
int main()
{
invoke_with_answer(callable_return_not_discardable); // note
}
Live demo on gcc-8
enum [[nodiscard]] Int : int{};
is also an option for integral types. At least you get conversions to anint
without too much fuss.
– StoryTeller
Nov 20 '18 at 12:35
1
@StoryTeller That's true :) I'll let it as-is since it's not the point, but true ^^.
– YSC
Nov 20 '18 at 12:36
add a comment |
As explained by StorryTeller, [[nodiscard]]
is not part of a function's signature or type, this is why that information is lost in the context of the template body.
A solution to get that warning propagated would be to add the [[nodiscard]]
attribute to the return type of that function:
template<class Callable>
void invoke_with_answer(Callable&& callable)
{ callable(42); } // warning
struct [[nodiscard]] Int { int value; };
Int callable_return_not_discardable(int n)
{ return {n}; }
int main()
{
invoke_with_answer(callable_return_not_discardable); // note
}
Live demo on gcc-8
As explained by StorryTeller, [[nodiscard]]
is not part of a function's signature or type, this is why that information is lost in the context of the template body.
A solution to get that warning propagated would be to add the [[nodiscard]]
attribute to the return type of that function:
template<class Callable>
void invoke_with_answer(Callable&& callable)
{ callable(42); } // warning
struct [[nodiscard]] Int { int value; };
Int callable_return_not_discardable(int n)
{ return {n}; }
int main()
{
invoke_with_answer(callable_return_not_discardable); // note
}
Live demo on gcc-8
answered Nov 20 '18 at 12:27
YSCYSC
23.7k553108
23.7k553108
enum [[nodiscard]] Int : int{};
is also an option for integral types. At least you get conversions to anint
without too much fuss.
– StoryTeller
Nov 20 '18 at 12:35
1
@StoryTeller That's true :) I'll let it as-is since it's not the point, but true ^^.
– YSC
Nov 20 '18 at 12:36
add a comment |
enum [[nodiscard]] Int : int{};
is also an option for integral types. At least you get conversions to anint
without too much fuss.
– StoryTeller
Nov 20 '18 at 12:35
1
@StoryTeller That's true :) I'll let it as-is since it's not the point, but true ^^.
– YSC
Nov 20 '18 at 12:36
enum [[nodiscard]] Int : int{};
is also an option for integral types. At least you get conversions to an int
without too much fuss.– StoryTeller
Nov 20 '18 at 12:35
enum [[nodiscard]] Int : int{};
is also an option for integral types. At least you get conversions to an int
without too much fuss.– StoryTeller
Nov 20 '18 at 12:35
1
1
@StoryTeller That's true :) I'll let it as-is since it's not the point, but true ^^.
– YSC
Nov 20 '18 at 12:36
@StoryTeller That's true :) I'll let it as-is since it's not the point, but true ^^.
– YSC
Nov 20 '18 at 12:36
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%2f53391788%2ftemplate-ignores-nodiscard-attribute%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
It has nothing to do with this being a template. If you replace your first line with
using Callable = int(int);
, you obtain exactly the same behavior. The behavior is a consequence (cf. StoryTellers answer) of what function pointers are.– Handy999
Nov 21 '18 at 7:17
@Handy Hi. Replying in the comment section is generally frowned upon. If you think you can improve an answer, suggest an edit or answer yourself.
– YSC
Nov 21 '18 at 19:31