473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

nonmember functions?

I have read that the allocator class has the following nonmember functions:

template <class In, class ForFor uninitialized_copy(In,In,For);
template <class For, class T>
void uninitialized_fill(For, For, const T&);

But what does it mean that the two functions are nonmembers? Are they
defined outside the class declaration in the header file?
May 23 '07 #1
8 2143
desktop wrote:
I have read that the allocator class has the following nonmember functions:

template <class In, class ForFor uninitialized_copy(In,In,For);
template <class For, class T>
void uninitialized_fill(For, For, const T&);

But what does it mean that the two functions are nonmembers? Are they
defined outside the class declaration in the header file?
It means the 2 functions are not members of the allocator class. They
could be declared as friend functions of the allocator class. As to
definition, they can be either inside or outside of the class declaration.

F
May 23 '07 #2

"Fei Liu" <fe****@aepnetworks.comwrote in message
news:f3**********@aioe.org...
desktop wrote:
>I have read that the allocator class has the following nonmember
functions:

template <class In, class ForFor uninitialized_copy(In,In,For);
template <class For, class T>
void uninitialized_fill(For, For, const T&);

But what does it mean that the two functions are nonmembers? Are they
defined outside the class declaration in the header file?

It means the 2 functions are not members of the allocator class. They
could be declared as friend functions of the allocator class.
They could be. They don't have to be. If the class has no private or
protected members which the functions need access to, then there's no need
to write friend declarations for them in the allocator class.
As to definition, they can be either inside or outside of the class
declaration.
Eh? How can you define a non-member function *inside* the class? Simply
declaring it inside the class (let alone defining it), makes it a member
function. (Unless, of course, you're talking about a friend declaration.
But that's still just a friend declaration, not a function definition.)

-Howard
May 23 '07 #3
Howard <al*****@hotmail.comwrote:
Eh? How can you define a non-member function *inside* the class? Simply
declaring it inside the class (let alone defining it), makes it a member
function. (Unless, of course, you're talking about a friend declaration.
But that's still just a friend declaration, not a function definition.)
Like this:

#include <iostream>

class Foo {
public:
Foo() : i_(42) { }

friend void print(const Foo& f)
{
std::cout << f.i_ << '\n';
}

private:
int i_;
};

int main()
{
Foo f;
print(f);
//f.print();
}
Uncomment f.print() to get a compilation error saying that print() is
not a member of Foo, but print() is defined inside the class.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 23 '07 #4
desktop wrote:
I have read that the allocator class has the following nonmember functions:

template <class In, class ForFor uninitialized_copy(In,In,For);
template <class For, class T>
void uninitialized_fill(For, For, const T&);

But what does it mean that the two functions are nonmembers? Are they
defined outside the class declaration in the header file?
It's a bit misleading. They're non-member functions in the sense that
they aren't member functions of any class. But they have no special
relationship to allocators. They're general purpose algorithms that
happen to be useful for allocators among other things.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
May 23 '07 #5

"Marcus Kwok" <ri******@gehennom.invalidwrote in message
news:f3**********@news-int2.gatech.edu...
Howard <al*****@hotmail.comwrote:
>Eh? How can you define a non-member function *inside* the class? Simply
declaring it inside the class (let alone defining it), makes it a member
function. (Unless, of course, you're talking about a friend declaration.
But that's still just a friend declaration, not a function definition.)

Like this:

#include <iostream>

class Foo {
public:
Foo() : i_(42) { }

friend void print(const Foo& f)
{
std::cout << f.i_ << '\n';
}

private:
int i_;
};

int main()
{
Foo f;
print(f);
//f.print();
}
Uncomment f.print() to get a compilation error saying that print() is
not a member of Foo, but print() is defined inside the class.
Interesting. Can't say as I've ever seen a friend function actually
*defined* inside a class. Is it portable? (It looks like this may be
related to a recent discussion about "injecting" a function into a namespace
via a friend declaration.) How is it affected by the visibility within that
class? Would it have to be in the a public section to be seen from outside,
even though it's not a member? Can it be specified via
Foo::print(whatever), or is it just a member of the enclosing namespace?

-Howard
May 23 '07 #6
Howard <al*****@hotmail.comwrote:
"Marcus Kwok" <ri******@gehennom.invalidwrote in message
news:f3**********@news-int2.gatech.edu...
>Howard <al*****@hotmail.comwrote:
>>Eh? How can you define a non-member function *inside* the class? Simply
declaring it inside the class (let alone defining it), makes it a member
function. (Unless, of course, you're talking about a friend declaration.
But that's still just a friend declaration, not a function definition.)

Like this:

#include <iostream>

class Foo {
public:
Foo() : i_(42) { }

friend void print(const Foo& f)
{
std::cout << f.i_ << '\n';
}

private:
int i_;
};

int main()
{
Foo f;
print(f);
//f.print();
}

Interesting. Can't say as I've ever seen a friend function actually
*defined* inside a class. Is it portable? (It looks like this may be
related to a recent discussion about "injecting" a function into a namespace
via a friend declaration.)
I can't say with 100% certainty, but I think it is. I first saw this
technique in a post here a while ago, but with a friend operator<<, and
nobody called him out on it.
How is it affected by the visibility within that
class? Would it have to be in the a public section to be seen from outside,
even though it's not a member?
I tried putting it in the private section and it still worked.
Can it be specified via
Foo::print(whatever), or is it just a member of the enclosing namespace?
I tried using it as Foo::print() and I got a compilation error saying
that print() is not a member of Foo, which makes sense since it is not a
class static function. Therefore, I would say that it is just a member
of the enclosing namespace.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 24 '07 #7
On May 23, 11:55 pm, "Howard" <alic...@hotmail.comwrote:
"Marcus Kwok" <ricec...@gehennom.invalidwrote in message
news:f3**********@news-int2.gatech.edu...
Howard <alic...@hotmail.comwrote:
Eh? How can you define a non-member function *inside* the class? Simply
declaring it inside the class (let alone defining it), makes it a member
function. (Unless, of course, you're talking about a friend declaration.
But that's still just a friend declaration, not a function definition.)
Like this:
#include <iostream>
class Foo {
public:
Foo() : i_(42) { }
friend void print(const Foo& f)
{
std::cout << f.i_ << '\n';
}
private:
int i_;
};
int main()
{
Foo f;
print(f);
//f.print();
}
Uncomment f.print() to get a compilation error saying that print() is
not a member of Foo, but print() is defined inside the class.
Interesting. Can't say as I've ever seen a friend function actually
*defined* inside a class. Is it portable?
[I thought I'd already answered this, but I don't see my
posting...]

It's a fairly well known idiom, first published, I think, by
Barton and Nackman. The committee took it into consideration
when changing the rules for name lookup; even then, breaking it
was considered to be something that would break too much code.
(It looks like this may be
related to a recent discussion about "injecting" a function into a namespace
via a friend declaration.)
Yup. Historically, it worked because friend names were injected
into the surrounding namespace scope (or file scope, given that
namespaces didn't exist). This injection caused problems
elsewhere (I forget what), and the committee did away with it.
But only because ADL could now be used to find the name.

Note that there are special cases which were broken. Consider
the following:

class A {} ;

class B
{
public:
B( A const& ) {}
friend void print( B const& ) {}
} ;

int main()
{
A anA ;
print( anA ) ;
return 0 ;
}

According to the old (pre-1998) rules, this is legal; print is
injected into the global namespace, name lookup in main finds
it, and overload resolution uses the convertion constructor to
convert the argument and call it. Under the new rules, there is
nothing in the arguments which relates to B, so the compiler
does not look there in ADL, and so the code fails to compile.

Many compilers (VC++ 8, Sun CC 5.8, g++ pre-4.0) still implement
friend injection; of the compilers I have access to, the only
one which complains about this code is g++ 4.1.0.
How is it affected by the visibility within that
class?
I think you mean access control. Visibility is a rather vague
term, related to name look up, and everything in a class
definition has the same "visibility". Access control (public,
private, etc.) only affects members, and a friend is not a
member.
Would it have to be in the a public section to be seen from outside,
even though it's not a member?
No.

Think about it for a minute. I can also declare the friend
outside of the class. Should it be accessible then, but not if
I don't.

A friend is not a member. Given something like:

namespace A {
class B {
friend void f() ;
} ;
}

The function declared by the friend declaration is A::f(), not
A::B::f(). Regardless of where it is defined. "Visibility"
(i.e. whether name lookup will find the symbol or not),
membership and access control or largely orthogonal issues. In
the example immediately above, f() is a member of A, and it's
fully qualified name is A::f(). It is, however, only visible in
contexts where name lookup looks into B: in member functions of
B, or when ADL kicks in for B. (Theoretically, it's also
visible in contexts like "someB.f()", after the dot. But in
those contexts, only members are considered, and since it's not
a member...)

Access control only affects members, and it is only applied
after name lookup and overload resolution have taken place; if
overload resolution chooses a private function, it's an error,
even if there is a public function of the same name which could
be called.
Can it be specified via
Foo::print(whatever), or is it just a member of the enclosing namespace?
It's just a member of the enclosing namespace. Even though it
isn't visible there:-).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 25 '07 #8
On 25 May 2007 00:05:16 -0700, James Kanze wrote:
>Note that there are special cases which were broken. Consider
the following:

class A {} ;

class B
{
public:
B( A const& ) {}
friend void print( B const& ) {}
} ;

int main()
{
A anA ;
print( anA ) ;
return 0 ;
}

According to the old (pre-1998) rules, this is legal; print is
injected into the global namespace, name lookup in main finds
it, and overload resolution uses the convertion constructor to
convert the argument and call it. Under the new rules, there is
nothing in the arguments which relates to B, so the compiler
does not look there in ADL, and so the code fails to compile.

Many compilers (VC++ 8, Sun CC 5.8, g++ pre-4.0) still implement
friend injection; of the compilers I have access to, the only
one which complains about this code is g++ 4.1.0.
And in the serious, responsible, tradition of g++ there's a temporary
option, -ffriend-injection, which can help in the transition:

<http://gcc.gnu.org/gcc-4.1/changes.html>

--
Gennaro Prota -- C++ Developer, For Hire
https://sourceforge.net/projects/breeze/
(please check 'Status <date>' link in Summary page)
May 25 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
13
by: Erik Haugen | last post by:
From reading gotw#84 (http://www.gotw.ca/gotw/084.htm), I'm convinced that I should try to make functions nonfriend nonmembers when practical, but then I came across this: Bruce Eckel says about...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
2
by: ma740988 | last post by:
So I'm reading the C++ coding standards(Shutter & Andrei), more specifically item 56. There's a statement: "Prefer to provide a nonmember swap function in the same namespace as your type when...
7
by: Tim ffitch | last post by:
Hi I have created a VB dll file that contains common functions I use across various projects in VB, Access and Excel. Rather than have to code the functions in each I decided to use the dll...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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,...
0
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.