473,795 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

nonmember functions?

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

template <class In, class ForFor uninitialized_c opy(In,In,For);
template <class For, class T>
void uninitialized_f ill(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 2160
desktop wrote:
I have read that the allocator class has the following nonmember functions:

template <class In, class ForFor uninitialized_c opy(In,In,For);
template <class For, class T>
void uninitialized_f ill(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****@aepnetw orks.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_c opy(In,In,For);
template <class For, class T>
void uninitialized_f ill(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*****@hotmai l.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_c opy(In,In,For);
template <class For, class T>
void uninitialized_f ill(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******@gehen nom.invalidwrot e in message
news:f3******** **@news-int2.gatech.edu ...
Howard <al*****@hotmai l.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(what ever), or is it just a member of the enclosing namespace?

-Howard
May 23 '07 #6
Howard <al*****@hotmai l.comwrote:
"Marcus Kwok" <ri******@gehen nom.invalidwrot e in message
news:f3******** **@news-int2.gatech.edu ...
>Howard <al*****@hotmai l.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(what ever), 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...@hotmai l.comwrote:
"Marcus Kwok" <ricec...@gehen nom.invalidwrot e in message
news:f3******** **@news-int2.gatech.edu ...
Howard <alic...@hotmai l.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(what ever), 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 objektorientier ter Datenverarbeitu ng
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
3349
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 charcters in it, that the code needs to run until all bad characters are gone? For example, if a file has the name "<bad*mac\file" the program has to run 3 times to get all three bad chars out of the file name. The passes look like this:
99
5926
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 important changes, so in this thread I would like to focus on that issue alone. I have edited the Proposed Syntax example below to take out the changes unecessary to this discussion. I left in the change of "instance variable" syntax (...
13
2445
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 operator overloads in Thinking In C++ (2nd Ed - http://www.codeguru.com/cpp/tic/tic0129.shtml) "In general, if it doesn't make any difference, they should be members, to emphasize the association between the operator and its class." That seems...
1
741
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 managed point-of-view I've noticed that: 1) for each managed and unmanaged C function (not C++ classes) I get a public managed static method (defined on a 'Global Functions' class) in the generated assembly with an export name of the form...
2
3792
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 produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently, Wang, Yu, and Lin showed a short- cut solution for finding collisions in SHA-1 . Their result
2
1983
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 objects of your type have a way to exchange their values more efficiently than via brute-force assignment, such as if they have their own swap or equivalent function. Additionally, consider specializing std::swap for your own template types:" ...
7
5907
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 route. The problem being that I can't call these functions from the query designer in Access. I decided that I would try the route of declaring the functions from the dll file the same way you would for the Windows API. Access then complains that...
23
4020
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 who will find them very helpfull. gcc has them as a non-standard option, but only when compiling C language code, so I'm afraid there might be some obscure reason why local functions are not so easy to be dealt with in C++, which I do not yet know.
7
3974
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 functions. Is it normal how C++ Compiler can compile large class without any problem? Didn't C++ Compiler have rules to limit the number of member functions? One big object has complex operations how member variables and member functions can be...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10438
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9042
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.