The scope of C ++ built-in functions

I get a compilation error:

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

in my built-in function, which is declared like this in the cpp file:

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

i has exactly the same function in another cpp file. can this cause a problem? how can i solve it?

+4
source share
11 answers

As litb and AndreyT point out, this answer does not address the actual problem - see the “Speakers” section for details.


While static , as Ophir said , gives you an internal connection, the "C ++ way" is to use unnamed namespaces:

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

§7.3.1.1 / 1:

A definition without namespace-name behaves as if it were replaced by

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

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

In §7.3.1.1 / 2 it is added:

Using the static keyword is not recommended when declaring objects in a namespace scope (see Appendix D); an unnamed namespace provides an excellent alternative.

+14
source

You have received incorrect answers in this thread. The problem in your code is that you define your inline function in your code twice:

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

inline will, as someone else would say, already protect you from an error that occurs when defining a function twice. You don't need static , and you won't need nameless namespaces. Also note that you should not define the built-in functions that are supposed to be used by other translation units (a file where all #include and #if, etc. are processed and expanded) in a .cpp file. Their definition should be known to all translation units that use them, so in this case their definition is indicated only in the header file - not both in the .cpp and the header file - this will lead to the error you get.

You also need heading protectors to avoid the above problem in each of your headings.

 // file foo.h #ifndef POINTS_H_INCLUDED #define POINTS_H_INCLUDED inline Boolean IsPointInRect(...) { ... } #endif // file bar.cpp #include "foo.h" // does not include the content a second time anymore - the guards aboid it: #include "foo.h" 

And you should not include the .cpp file in another .cpp file ( .cpp files should not be included). You must include the header files in the .cpp files.

+10
source

placing them in appropriate namespaces makes it a simple solution. Even Anonymous will work.

+6
source

As others have said, if you really want to limit the scope of a function to a cpp file, you should put it in a namespace, for example

 namespace // anonymous { inline bool IsPointInRect(...) { ... } } 

However, it would seem that the maintenance problem should have the same function with the same body (that is, they both do the same) in two different places - copy / paste the style. If two different functions have the same name but different bodies, then this is also a problem waiting for this to happen.

Obviously, you have performance issues (hence built-in), but it would be best to have a function written in one place! At a minimum, put it in the header file (in the anonymous namespace), which is included where you need it. In fact, you should do it “cleanly” and then return to specific performance approaches when your profiler tells you.

Greetings

 Michael 
+3
source

If you move them to a single header file that should solve it. Or declare one or both of them static . Or, if you are using MSVC, declare them __declspec(selectany) .

+2
source

Save your built-in functions in the header file and use include guard to make sure the file is not included twice.

+1
source

I have exactly the same function

Do you mean a function with the same name and type, or do you mean a function with the same name, type and body? There is nothing wrong with the latter, in fact, what inline does - it allows you to identify the same function identically in different translation units. For the first, you must make sure that the function has a file size. inline does not provide file scope, static and unnamed namespaces. An invalid namespace is the preferred way to do this in C ++, at least according to the standard: static deprecated.

+1
source

Get rid of one of them? What did you expect?

Perhaps you should explain why you need two versions ...

0
source

You can add “static” or “extern” before the “inline” I think (I know you can use GCC), for example:

 static inline Boolean IsPointInRect(...) { ... } 
0
source

There are two ways to declare built-in functions:

  • Inside a class: Any function defined inside a class is a built-in function.

  • Outside the class: if you define it outside the class, you need to use the 'inline' keyword along with the function signature.

If you have another function with the same name as in another class, it will not throw any exceptions if you use the region resolution operator (: :) and dot (.) Correctly with the object name.

0
source

1. Use the extern and static keywords

2.declare inside the namespace

0
source

Source: https://habr.com/ru/post/1303101/


All Articles