8

I read that C++11 has introduced the concept of attributes for example [[noreturn]] which is to indicate that the function doesn not return to the caller.

[[noreturn]] void fun() 
{
    throw std::string("Error!!!");
}

void func()
{
    fun();
}

void aTempFunc()
{
    try
    {
        func();
    }
    catch (std::string &e)
    {
        std::cout << e << std::endl;
    }
}

By looking at the example the reader can understand that the function throws an exception and call will not be returned to the func function. I am bit confused to understand what are C++ attributes and why it is required? How a programmer can really make use of these attributes?

Can someone explain in detail. Please correct me if my understanding about the attributes is wrong. Thanks.

NJMR
  • 1,886
  • 1
  • 27
  • 46
  • 1
    You can [read about attributes in the documentation](http://en.cppreference.com/w/cpp/language/attributes). It describes the behavior and it should become clear why you would need them and what you need to consider when doing so. Do you have a more specific question about one of them? – nwp Dec 05 '17 at 15:03
  • 4
    What if `fun` is defined somewhere in a library for which you do not have the source code? – Quentin Dec 05 '17 at 15:07
  • While it's can be useful information for programmers, it's definitively useful information for compilers. – François Andrieux Dec 05 '17 at 15:09
  • @Quentin: Got it. Since I cannot see the source code, I cannot see [[noreturn]] also. But how do I know that the function will not return to me. – NJMR Dec 05 '17 at 15:10
  • 1
    It would be more useful with `int func() { fun(); }`, where the compiler can realize that the missing `return` in `func` isn't really an error, as it never returns at all. – Bo Persson Dec 05 '17 at 15:10
  • 1
    @NJMR you can see the header - just not the implementation... – UKMonkey Dec 05 '17 at 15:10
  • 2
    Are you asking about `[[noreturn]]` or about attributes in general? The answer would be very different in either case. – anatolyg Dec 05 '17 at 15:11
  • @anatolyg: Oops, sorry for the grammer. I wanted to understand the attributes in general. – NJMR Dec 05 '17 at 15:19

2 Answers2

4

Most of the attributes are compiler hint, ABI specification or requirement relative to the target object format (visibility, section, etc.).

So most attributes do not change the observable behavior of your program: if you remove all the attributes of your source code, and if it compiles, you can reasonably expect to have a resulting program behaving as the one compiled with the attributes.

But there are also attributes that can noticeably affect the behavior or compilability of your program, for example the align attribute, even if they do not change drastically the semantic of your code.

Implementation can provide their own attributes, and these attributes can have any consequence as long as the observable behaviors of the program follow the c++ language, C++ standard[intro.compliance]:

A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any well-formed program. Implementations are required to diagnose programs that use such extensions that are ill-formed according to this document. Having done so, however, they can compile and execute such programs.

Oliv
  • 17,610
  • 1
  • 29
  • 72
  • Actually, `carries_dependency` does *not* affect the semantics of the program. It only enables certain (platform-dependent) compiler optimizations, *if* the function is used in a multi-threaded environment in conjunction with `memory_order_consume` operations. See the accepted answer to https://stackoverflow.com/questions/6411270/what-does-the-carries-dependency-attribute-mean – Arne Vogel Dec 06 '17 at 15:33
3

attributes are put by the programmer, for the compiler benefit. Basically, they give the compiler more information and allow for more optimization.

For example the noreturn attribute allow the compiler to reclaim stack memory before calling the function (because you will not need the stack frames again) or at least consider the code following the call as dead code.

pqnet
  • 6,070
  • 1
  • 30
  • 51