Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ scope of inline functions

Tags:

c++

inline

i am getting the compile error:

Error 7 error C2084: function 'Boolean IsPointInRect(...)' already has a body

on my inline function, which is declared like this in a cpp file:

inline Boolean IsPointInRect(...) 
{
...
}

i have exactly the same function in another cpp file. might this be causing the problem? how can i solve it?

like image 772
clamp Avatar asked Nov 28 '22 01:11

clamp


1 Answers

As litb and AndreyT point out, this answer doesn't address the actual problem - see litbs answer for details.


While static, as Ofir said, gives you internal linkage, the "C++ way" is to use unnamed namespaces:

namespace
{
     inline Boolean IsPointInRect(/*...*/) { /*...*/ }
}

§7.3.1.1/1:

An unnamed-namespace-definition behaves as if it were replaced by

 namespace unique { /* empty body */ }
 using namespace unique; 
 namespace unique { namespace-body }

where all occurrences of unique in a translation unit are replaced by the same identifier and this identifier differs from all other identifiers in the entire program.

§7.3.1.1/2 adds:

The use of the static keyword is deprecated when declaring objects in a namespace scope (see annex D); the unnamed-namespace provides a superior alternative.

like image 155
Georg Fritzsche Avatar answered Dec 28 '22 17:12

Georg Fritzsche