Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pure/const functions in C++0x

In C++98/C++03, there are no pure/const function keywords in the language.

  1. Has this changed in C++0x?

  2. If so, is it possible to set such a flag even on function objects (std::function)? So I can pass some function pointer or lambda functions and additional give the information that it is a pure/const function? The called function may have an optimized execution path for such a function.

  3. Is there some way to check if a given function is pure/const? I.e. for example, if there is such flag on std::function as described above, I probably could just check that flag. But maybe there is even a more general way.

If it has not changed, why not? I think it might be quite useful to have such a support.

Are there any open proposals about it?

like image 450
Albert Avatar asked Sep 26 '10 21:09

Albert


People also ask

What is the use of const functions in C++?

const member functions Declaring a member function with the const keyword specifies that the function is a "read-only" function that doesn't modify the object for which it's called. A constant member function can't modify any non-static data members or call any member functions that aren't constant.

What does const mean after a function in C++?

Const member functions in C++ The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided.

Can a const function call a non const function?

A const object can be created by prefixing the const keyword to the object declaration. Any attempt to change the data member of const objects results in a compile-time error. When a function is declared as const, it can be called on any type of object, const object as well as non-const objects.


2 Answers

  1. Has this changed in C++0x?

No. There is a constexpr but it means compile time constant. If its parameters are constexprs too then it's executed at compile time, but it's a regular function otherwise. Since they must be defined in the same translation unit and consist of a single return statement they probably will be inlined and the above optimization will be performed. It can't be used to provide the compiler information about externally linked function.

If it has not changed, why not? I think it might be quite useful to have such a support.

Actually I don't think you need it. The language is already too big, and the programmer can easily rewrite this code to be more efficient based on her knowledge anyway. Unlike restrict it doesn't provide any info that can't be expressed by other means.

Are there any open proposals about it?

I haven't seen any committee papers on that topic.

like image 70
Yakov Galka Avatar answered Sep 29 '22 05:09

Yakov Galka


gcc uses __attribute__(( <attr> )) to define extra attributes on functions.

  • pure: only accesses (but does not modify) parameters and global memory. GCC uses this information to determine if the optimizer can completely omit repeated calls to the function (local-memoization). Two notable pure functions are strlen and memcmp.

  • const: not to be confused with C++ const, const functions only accesses parameters and those parameters must not be pointers. It is basically a more restricted version of pure. The optimizer treats const functions that same as pure. Though in theory it could perform more aggressive (non-local) memoization than it does for pure.

C++11's new attribute syntax (§7.6) was designed to do just this sort of thing. Currently you cannot use C++'s attribute syntax to set GCC attributes, but that will change in future gcc versions.

So you will be able to assign the pure attribute to functions using the attribute syntax. But there isn't a standard pure attribute. pure will be compiler specific, but it will do the right thing on gcc.

For the curious, here is the list of the standard attributes:

  • align
  • noreturn
  • override
  • hiding
  • base_check
  • carries_dependency
like image 30
deft_code Avatar answered Sep 29 '22 05:09

deft_code