Template ignores [[nodiscard]] attribute












6















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?










share|improve this question























  • 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
















6















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?










share|improve this question























  • 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














6












6








6








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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



















  • 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

















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












2 Answers
2






active

oldest

votes


















8














[[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.






share|improve this answer


























  • @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



















2














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






share|improve this answer
























  • 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





    @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











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%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









8














[[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.






share|improve this answer


























  • @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
















8














[[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.






share|improve this answer


























  • @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














8












8








8







[[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.






share|improve this answer















[[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.







share|improve this answer














share|improve this answer



share|improve this answer








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



















  • @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













2














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






share|improve this answer
























  • 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





    @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
















2














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






share|improve this answer
























  • 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





    @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














2












2








2







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










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 an int 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






  • 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


















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.




draft saved


draft discarded














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





















































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

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?