local functions, namespaces, and inlining | | |
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 | | | | re: local functions, namespaces, and inlining
David Rubin wrote:[color=blue]
> 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
>[/color]
Why not define it where you declare it?
namespace
{
inline int port2path( int port )
{
int path = 0;
// do stuff
return path;
}
} | | | | re: local functions, namespaces, and inlining
Jeff Schwab wrote:
[color=blue][color=green]
> > file.cc
> > -------
> > namespace {
> > inline int port2path(int port);
> > };
> >
> > /* ... */
> >
> > int port2path(int port) {...}[/color][/color]
[snip][color=blue]
> Why not define it where you declare it?
>
> namespace
> {
> inline int port2path( int port )
> {
> int path = 0;
> // do stuff
> return path;
> }
> }[/color]
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 | | | | re: local functions, namespaces, and inlining
"David Rubin" <bogus_address@nomail.com> wrote in message news:400438A9.26EB6C57@nomail.com...[color=blue]
> 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.[/color]
Only static objects are deprecated.
[color=blue]
> 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);
> };
>[/color]
[color=blue]
> int port2path(int port) {...}[/color]
This is fine.... there is an implicit using when declaring unnamed namespaces. | | | | re: local functions, namespaces, and inlining
On Tue, 13 Jan 2004 14:17:39 -0500 in comp.lang.c++, David Rubin
<bogus_address@nomail.com> was alleged to have written:[color=blue]
>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.[/color]
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. | | | | re: local functions, namespaces, and inlining
David Rubin wrote:[color=blue]
> Jeff Schwab wrote:
>
>[color=green][color=darkred]
>>>file.cc
>>>-------
>>>namespace {
>>> inline int port2path(int port);
>>>};
>>>
>>>/* ... */
>>>
>>>int port2path(int port) {...}[/color][/color]
>
>
> [snip]
>[color=green]
>>Why not define it where you declare it?
>>
>>namespace
>>{
>> inline int port2path( int port )
>> {
>> int path = 0;
>> // do stuff
>> return path;
>> }
>>}[/color]
>
>
> That is part of my question. Presumably, I have a lot of local functions[/color]
Presumably? Do you or don't you? You said:
"I want to define an inline function"
[color=blue]
> 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.[/color]
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? | | | | re: local functions, namespaces, and inlining
Ron Natalie wrote:[color=blue]
> "David Rubin" <bogus_address@nomail.com> wrote in message news:400438A9.26EB6C57@nomail.com...
>[color=green]
>>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.[/color]
>
>
> Only static objects are deprecated.
>
>[color=green]
>>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);
>>};
>>[/color]
>
>[color=green]
>>int port2path(int port) {...}[/color]
>
>
> This is fine.... there is an implicit using when declaring unnamed namespaces.
>[/color]
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 | | | | re: local functions, namespaces, and inlining
"Jeff Schwab" <jeffplus@comcast.net> wrote in message news:886dnfZKB6aHzpnd4p2dnA@comcast.com...[color=blue]
> 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.
>[/color]
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; }
} | | | | re: local functions, namespaces, and inlining
Ron Natalie wrote:[color=blue]
> "Jeff Schwab" <jeffplus@comcast.net> wrote in message news:886dnfZKB6aHzpnd4p2dnA@comcast.com...
>[color=green]
>>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.
>>[/color]
>
> 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; }
> }[/color]
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." |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|