local functions, namespaces, and inlining

D

David Rubin

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
 
J

Jeff Schwab

David said:
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;
}
}
 
D

David Rubin

Jeff said:
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
 
R

Ron Natalie

David Rubin said:
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
int port2path(int port) {...}

This is fine.... there is an implicit using when declaring unnamed namespaces.
 
D

David Harmon

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.
 
J

Jeff Schwab

David said:
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?
 
J

Jeff Schwab

Ron said:
Only static objects are deprecated.




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
 
R

Ron Natalie

Jeff Schwab said:
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; }
}
 
D

David Rubin

Ron said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top