21

Can I declare a non-member function (global function, may be) as const in C++? I understand that the const keyword actually is applied to the implicit "this" argument passed in member functions. Also since only member functions follow the "thiscall" calling convention, can const be applied for non-member functions?

Leaving aside what I am trying to do by declaring non-member function const, would compiler report error for doing so?

Sulla
  • 7,631
  • 9
  • 45
  • 71
  • 6
    "Leaving aside..." I don't see how it can be left aside, really. As for whether the compiler would report an error, why don't you just **try it**? – Karl Knechtel Dec 08 '10 at 12:02
  • 1
    @Karl Knechtel: because some compilers could (in theory) issue a warning instead of an error, or accept it as an extension? – MSalters Dec 08 '10 at 14:28
  • @Karl: gcc has `__pure__` and `__const__` attributes for this task, and they are attributes because they are non-standard. – Matthieu M. Dec 08 '10 at 14:53
  • @Matthieu, depending on your understanding of what "this task" is. – Karl Knechtel Dec 08 '10 at 16:08
  • @Karl: yes, obviously :) – Matthieu M. Dec 08 '10 at 17:54
  • If you insist on the *oop* tag, then Mephane is correct. But if you accept that the *oop* tag here is an error and that I am not commenting this a single minute to late, then [this question](https://stackoverflow.com/q/13674767/2712726) has answers that get you as far as you can get with c++ today. – Patrick Fromberg Mar 28 '21 at 20:03

2 Answers2

24

No, only a non-static member function may be const qualified.

What semantic would you expect from a const non-member function ? If you want to enforce that no parameters are modified by the function, just take them by const reference.

icecrime
  • 74,451
  • 13
  • 99
  • 111
  • 3
    I'd assume he'd like to have the compiler make sure a global (const) function won't modify any global variable. That said, since this question is tagged 'c++' and 'oop' - just don't do it. It's ok to have a global (or static class member) object where it makes sense, but it's not a good idea to write functions that have side-effects outside of their class instance's scope. – Mephane Dec 08 '10 at 13:18
  • It is possible to achieve the same effect by qualifying the each formal argument as const, ex: int foo(const arg1, const arg2, ...) Means, const for member function is actually a way of telling the implicit argument "this" is a const. For non member function, there is already way to do so by using const in function declaration for each arguments (if one likes). Further, since there is no implicit argument (like 'this') in non member function , it does not have meaning. – Prabhu U Jan 02 '22 at 17:42
  • Then why are there const non-member functions like find? https://en.cppreference.com/w/cpp/string/basic_string/find – JoeManiaci Feb 10 '23 at 19:06
2

To answer your second question: an attempt to use the member function syntax for a non-member (i.e. void foo() const; ) is a grammar violation. Therefore, a compiler must give a diagnostic - either an error or a warning. It may not silently ignore the const. However, it may report a warning, then pretend the const wasn't there and produce an executable.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • 2
    Wait, the compiler is allowed to demote grammar violations (called "syntax errors" in any other context I can think of) to warnings? – Karl Knechtel Dec 08 '10 at 16:07
  • Yes. The only difference between an error and a warning is that you don't get an executable if the compiler encountered an error. The standard only speaks about diagnostics because it doesn't care about this difference. E.g. the grammar violation/syntax error "virtual ~Foo() = 0L;" may cause the warning "pure virtual function must be declared as virtual ~Foo() = 0;". – MSalters Dec 09 '10 at 15:41