What is COAP?
What is COAP?
May 31, 2022
What is the difference between testing and debugging?
What is the difference between testing and debugging?
June 14, 2022

June 7, 2022

C++17: [[nodiscard]] attribute

C++17 introduces [[nodiscard]] attribute to let the compiler deduce that the return value of a function or method must be handled by a properly assigned value. The attribute simply produces compilation warning when the flagged function is invoked with its return value bypassed.

#include <cstring>
[[nodiscard ( Deallocate the char* in callers scope using
             delete[]!")]]
char getLog() {
     char* log
     new char[100];
     strcpy(log, "[INFO]: Hello World!");
     return log;
}

int main() {
    getLog(); // ERROR: attribute produces
             // compile time warning
    auto log = getLog(); // OK
    delete[] log;
    return 0;
}
C++17: [[nodiscard]] attribute
This website uses cookies to improve your experience. By using this website you agree to our Data Privacy Statement
Read more