I want to define an inline function, but I need it to be local to my
translation unit. C++PL3ed indicates that the use of 'static' is
deprecated in favor of (unnamed) namespaces. I am confused about where
to put the declaration vs the definition. For example, I think this is
correct:
file.cc
-------
namespace {
inline int port2path(int port);
};
/* ... */
int port2path(int port) {...}
Note that I cannot define port2path in, e.g., file.h, because of the way
file.h is (improperly) used in my codebase. In any case, is it correct
to only declare the function in an unnamed namespace (with inline) and
then define it elsewhere in the file (without inline)?
/david
--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown 8 2239
David Rubin wrote: I want to define an inline function, but I need it to be local to my translation unit. C++PL3ed indicates that the use of 'static' is deprecated in favor of (unnamed) namespaces. I am confused about where to put the declaration vs the definition. For example, I think this is correct:
file.cc ------- namespace { inline int port2path(int port); };
/* ... */
int port2path(int port) {...}
Note that I cannot define port2path in, e.g., file.h, because of the way file.h is (improperly) used in my codebase. In any case, is it correct to only declare the function in an unnamed namespace (with inline) and then define it elsewhere in the file (without inline)?
/david
Why not define it where you declare it?
namespace
{
inline int port2path( int port )
{
int path = 0;
// do stuff
return path;
}
}
Jeff Schwab wrote: file.cc ------- namespace { inline int port2path(int port); };
/* ... */
int port2path(int port) {...}
[snip] Why not define it where you declare it?
namespace { inline int port2path( int port ) { int path = 0; // do stuff return path; } }
That is part of my question. Presumably, I have a lot of local functions
and variables, so it may be more manageable to separate the declarations
from the definitions, especially if I decide to change the scope of some
subset of functions.
/david
--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
"David Rubin" <bo***********@nomail.com> wrote in message news:40***************@nomail.com... I want to define an inline function, but I need it to be local to my translation unit. C++PL3ed indicates that the use of 'static' is deprecated in favor of (unnamed) namespaces.
Only static objects are deprecated.
I am confused about where to put the declaration vs the definition. For example, I think this is correct:
file.cc ------- namespace { inline int port2path(int port); };
int port2path(int port) {...}
This is fine.... there is an implicit using when declaring unnamed namespaces.
On Tue, 13 Jan 2004 14:17:39 -0500 in comp.lang.c++, David Rubin
<bo***********@nomail.com> was alleged to have written: That is part of my question. Presumably, I have a lot of local functions and variables, so it may be more manageable to separate the declarations from the definitions, especially if I decide to change the scope of some subset of functions.
In my opinion, it is a LOT more manageable to NOT give separate
declarations and definitions for local functions - it's unnecessary
duplication. Just put the definition before the use and all is good.
David Rubin wrote: Jeff Schwab wrote:
file.cc ------- namespace { inline int port2path(int port); };
/* ... */
int port2path(int port) {...}
[snip]
Why not define it where you declare it?
namespace { inline int port2path( int port ) { int path = 0; // do stuff return path; } }
That is part of my question. Presumably, I have a lot of local functions
Presumably? Do you or don't you? You said:
"I want to define an inline function"
and variables, so it may be more manageable to separate the declarations from the definitions, especially if I decide to change the scope of some subset of functions.
I thought the scope of your function(s) was, by definition, local to one
translation unit? Are you going to try to limit the scope of the
functions within the TU?
Ron Natalie wrote: "David Rubin" <bo***********@nomail.com> wrote in message news:40***************@nomail.com...
I want to define an inline function, but I need it to be local to my translation unit. C++PL3ed indicates that the use of 'static' is deprecated in favor of (unnamed) namespaces.
Only static objects are deprecated.
I am confused about where to put the declaration vs the definition. For example, I think this is correct:
file.cc ------- namespace { inline int port2path(int port); };
int port2path(int port) {...}
This is fine.... there is an implicit using when declaring unnamed namespaces.
But does that mean you're defining the function declared in the
anonymous namespace? GCC doesn't think so... I'm not sure where the
standard stands on this.
namespace
{
inline int port2path( int port );
}
int port2path( int port )
{
return port;
}
int main( )
{
port2path( 3 );
}
g++ -c -o main.o main.cc
main.cc: In function `int main()':
main.cc:14: error: call of overloaded `port2path(int)' is ambiguous
main.cc:7: error: candidates are: int port2path(int)
main.cc:3: error: int <unnamed>::port2path(int)
make: *** [main.o] Error 1
"Jeff Schwab" <je******@comcast.net> wrote in message news:88********************@comcast.com... But does that mean you're defining the function declared in the anonymous namespace? GCC doesn't think so... I'm not sure where the standard stands on this.
Duh, you're right.
All you need do is define the function inside another unnamed namespace
definition:
namespace { inline int port2path(int); }
namespace {
int port2path(int port) { return port; }
}
Ron Natalie wrote: "Jeff Schwab" <je******@comcast.net> wrote in message news:88********************@comcast.com...
But does that mean you're defining the function declared in the anonymous namespace? GCC doesn't think so... I'm not sure where the standard stands on this.
Duh, you're right.
All you need do is define the function inside another unnamed namespace definition:
namespace { inline int port2path(int); }
namespace { int port2path(int port) { return port; } }
In that case, I /would/ agree with Jeff and David Harmon. If you have to
define the function within an unnamed namespace, you might as well use
just one. Thanks for the replies.
BTW, I found out the compiler for my project does not support namespaces
anyway! So, I have to use 'static'. Oh well...
/david
--
"As a scientist, Throckmorton knew that if he were ever to break wind in
the echo chamber, he would never hear the end of it." This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andrey Tatarinov |
last post by:
Hi.
It would be great to be able to reverse usage/definition parts in
haskell-way with "where" keyword. Since Python 3 would miss lambda, that
would be extremly useful for creating readable...
|
by: Dave Townsend |
last post by:
Yo,
I had a job interview today, the interviewing asked me about
inline virtual functions, or what was my opinion on them.
Hm, I've seen mention of these babies in the reference material,
but...
|
by: Rubén Campos |
last post by:
I haven't found any previous message related to what I'm going to ask here,
but accept my anticipated excuses if I'm wrong.
I want to ask about the real usefulness of the 'inline' keyword. I've...
|
by: MrThingy |
last post by:
How can I have a function within a class that I know is going to be
called almost thousands of times during a loop cycle and have it run as
fast as possible, without using bug-prone #define macros?...
|
by: Raphael |
last post by:
Hello,
I have 2 files
A.c : Defining a bunch of usefull functions
B.c : Defining a main that uses these ones.
I need to improve perfs :
I know that, if the caller and the functions called...
|
by: Sheila Jones |
last post by:
Hello,
Can anybody tell me if the C# compiler will automatically inline simple
functions? To be specific, given:
double RadiansToDegrees(double degrees) {
return degrees*180.0/Math.PI;
}
...
|
by: Bilgehan.Balban |
last post by:
Hi,
If I define an inline function in one .c file, and use it from another,
after compiling and linking the two, it seems the function is not
inlined but rather called as a regular function. I...
|
by: Mike |
last post by:
Hi !
I have some strange problem and I would like to know if it is a bug or not :
In my projects, in 2 different .cpp files, I use the same name to define a
local structure:
file1.cpp :
...
|
by: newbie |
last post by:
I happened to read boost library code and realized that most (the part
I read) functions are inlined like:
template <class Key>
inline void Foo(const Key& k) {
...
...
}
Is there a strong...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| | |