plus( 2, 3 )

S

Stefan Ram

Here is a program. It does not seem to be correct, but you might
still get the idea of what I would like to accomplish using »plus«.
Can you modify my program so that it prints the sum (5) of 2 and 3
using »plus« in the simplemost way possible, in a way that is as
close to the following program as posssible?

#include <iostream>
#include <ostream>
#include <functional>

int main(){ ::std::cout << ::std::plus( 2, 3 )<< '\n'; }
 
I

Ian Collins

Stefan said:
Here is a program. It does not seem to be correct, but you might
still get the idea of what I would like to accomplish using »plus«.
Can you modify my program so that it prints the sum (5) of 2 and 3
using »plus« in the simplemost way possible, in a way that is as
close to the following program as posssible?

#include <iostream>
#include <ostream>
#include <functional>

int main(){ ::std::cout << ::std::plus( 2, 3 )<< '\n'; }

The use the superfluous form "::std"?

I don't think std::plus is intended to be used in the way you want, it
is designed for use in algorithms. Anyway, how about:

template <typename T> T
plus( const T& x, const T& y ) {return std::plus<T>()( x, y ); }

int main(){ std::cout << plus( 2, 3 )<< '\n'; }
 
S

Stefan Ram

Ian Collins said:
The use the superfluous form "::std"?

The following program prints »example«.

#include <iostream>
#include <ostream>

namespace alpha
{ namespace std {} void example() { ::std::cout << "example\n"; }}

int main(){ ::alpha::example(); }

I believe it will not do so anymore when »::std« is replaced
by »std«. This is because

»std« means »any nearby namespace "std"«, while
»::std« means »the famous standard namespace "std"«.

Since I wanted to refer to the standard namespace in this
case, I chose »::std«.
I don't think std::plus is intended to be used in the way you want, it
is designed for use in algorithms. Anyway, how about:
template <typename T> T
plus( const T& x, const T& y ) {return std::plus<T>()( x, y ); }
int main(){ std::cout << plus( 2, 3 )<< '\n'; }

Thanks! That helped me to find what I was looking for:

#include <iostream>
#include <ostream>
#include <functional>

int main(){ std::cout << ::std::plus< int >()( 2, 3 )<< '\n'; }

. I thought that I had tried this one before I send my post,
but I must have gotten some detail wrong.
 
Ö

Öö Tiib

The following program prints »example«.

#include <iostream>
#include <ostream>

namespace alpha
{ namespace std {} void example() { ::std::cout << "example\n"; }}

int main(){ ::alpha::example(); }

I believe it will not do so anymore when »::std« is replaced
by »std«. This is because

»std« means »any nearby namespace "std"«, while
»::std« means »the famous standard namespace "std"«.

Technically 'std' may mean any name 'std' in code. Most
developers however prefer to use their resources and influence
to make author of any alternative 'std' in C++ code they have
to work with to suffer and such work undone. Therefore the
form '::std' makes sense in theory but contains superfluous
pair of colons in practice. ;)
 
S

Stefan Ram

Öö Tiib said:
Technically 'std' may mean any name 'std' in code. Most
developers however prefer to use their resources and influence
to make author of any alternative 'std' in C++ code they have
to work with to suffer and such work undone. Therefore the
form '::std' makes sense in theory but contains superfluous
pair of colons in practice. ;)

| »Whenever a name x defined in the standard library is
| mentioned, the name x is assumed to be fully qualified
| as ::std::x, unless explicitly described otherwise.«

N3797, 17.6.1.1p3

So, you see that N3797 sometimes uses »::std« itself.

And speaking of colons: »was: « also requires one.

| In the following examples, please note that only "Re: " has any
| official status (and hence may be utilized by reading agents).
| "was: " is a convention used by many English-speaking posters to
| signal a change in subject matter. Software can always recognize
| that such changes have occurred from the References header.
|
| Subject: Film at 11
| Subject: Re: Film at 11
| Subject: Godwin's law considered harmful (was: Film at 11)
| Subject: Godwin's law (was: Film at 11)
| Subject: Re: Godwin's law (was: Film at 11)
| Subject: Re: Godwin's law

C. H. LINDSEY
2005-03-16
Usenet Best Practice
draft
3.2.1.1.1. Examples
 
Ö

Öö Tiib

| »Whenever a name x defined in the standard library is
| mentioned, the name x is assumed to be fully qualified
| as ::std::x, unless explicitly described otherwise.«

N3797, 17.6.1.1p3

So, you see that N3797 sometimes uses »::std« itself.

Indeed. I did not attempt to say that the colons may not
be used. If it is your style then use. Also woodbrian uses
them AFAIK.

What I attempted to say was that there are no reasons to fully
qualify 'std' in practice because that means thousands of
colons that disambiguate nothing. They disambiguate nothing
because people avoid using name 'std' for anything else in
practice. At least I can't detect any cases in C++ code that I
can find. I can't even find name 'boost' used for anything
else but as namespace of boost libraries.
And speaking of colons: »was: « also requires one.

Yes. Thanks for fixing, my bad.
 
W

woodbrian77

Indeed. I did not attempt to say that the colons may not
be used. If it is your style then use. Also woodbrian uses
them AFAIK.

They may be mostly superfluous, but other std namespaces
could be introduced through ignorance or evil intent.

If you are working on library development, you
can't know all the other software that your
software will be used with.

These guys weren't paranoid and they got burned:
http://www.cbsnews.com/news/is-the-us-stock-market-rigged/

Brian
Ebenezer Enterprises - In G-d we trust.
http://webEbenezer.net
 
N

Norman J. Goldstein

Here is a program. It does not seem to be correct, but you might
still get the idea of what I would like to accomplish using »plus«.
Can you modify my program so that it prints the sum (5) of 2 and 3
using »plus« in the simplemost way possible, in a way that is as
close to the following program as posssible?

#include <iostream>
#include <ostream>
#include <functional>

int main(){ ::std::cout << ::std::plus( 2, 3 )<< '\n'; }

This works (add the template type and default constructor):

int main(){ ::std::cout << ::std::plus<int>()( 2, 3 )<< '\n'; }
 

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