Questions tagged [non-member-functions]
98 questions
8
votes
3 answers
Friend functions of a class template
I have a class template Foo.
I'd like to implement a non-member function Bar that takes two Foos and returns a Foo. I want Bar to be a non-member because it will be more natural for callers to write Bar(f1, f2) than f1.Bar(f2). I also want Bar…

Adrian McCarthy
- 45,555
- 16
- 123
- 175
7
votes
3 answers
C++: Difference Between Non-Member Function and Static Member Function?
Simple question, here: what is the difference between a static member function, i.e. a function that can be called without requiring an object to access it (simply using the class identifier), and a non-member function? Here, I am asking both…

Thomas
- 6,291
- 6
- 40
- 69
7
votes
3 answers
Why are C++11 string new functions (stod, stof) not member functions of the string class?
Why are those C++11 new functions of header (stod, stof, stoull) not member functions of the string class ?
Isn't more C++ compliant to write mystring.stod(...) rather than stod(mystring,...)?

Greg82
- 1,000
- 3
- 10
- 24
7
votes
3 answers
ADL in case of equal-named member function
The situation is that some member function bar::Bar::frobnicate wants to utilize ADL to find a function from some unknown namespace, within a function that has an identical name. However, it only finds its own name.
Testcase
(Note that in reality,…

Sebastian Mach
- 38,570
- 8
- 95
- 130
6
votes
1 answer
Are begin(container) and end(container) standardized?
Are the non-member function templates begin(container) and end(container) part of C++0x? If so, in which header file do they live?

fredoverflow
- 256,549
- 94
- 388
- 662
6
votes
3 answers
Slicing and operator overloading in C++
Background Info
I've been programming in Java for a while, and I've only switched over to C++ just a few months ago, so I apologize if the answer is just something silly that I missed! Now that that's all been said, it is time for the issue at hand!…

gen220
- 63
- 1
- 4
5
votes
3 answers
What's the syntax to overload operator== as a free function with templated parameters?
I have a set of polymorphic classes, such as:
class Apple {};
class Red : public Apple {};
class Green : public Apple {};
And free functions which compare them:
bool operator==(const Apple&, const Apple&);
bool operator< (const Apple&, const…

Kyle
- 4,487
- 3
- 29
- 45
5
votes
3 answers
Support of std::cbegin() in C++14
Item 13 from Scott Mayers' "Effective Modern C++" states to prefer const_iterators over iterators. I agree but I also want to use non-member functions rather than member functions. According to the book there should be a non-member function…

Michiel uit het Broek
- 983
- 2
- 7
- 29
5
votes
2 answers
begin() and end() free function overload on template
I have a templated class, Iterable; for which I want to overload the begin() and end() free functions. It stores data as a vector of unique_ptr, but the interface uses boost::indirect_iterator for convenience.
My code builds and runs under…

Jean-Michaël Celerier
- 7,412
- 3
- 54
- 75
4
votes
1 answer
Can the standard allow (or would run into contradictions) calling a member function as if it was free function?
A member function pointer must be invoked using the .* (or ->*) syntax, so it can't be passed to a higher-order function:
#include
void for_each(auto const& v, auto f) {
for (auto const& e : v)
f(e); // error: must use '.*' or '->*'…

Enlico
- 23,259
- 6
- 48
- 102
4
votes
2 answers
Why does std::iterator not contain std::prev() as a member function?
it++; // OK : Widely used expression for moving iterator.
it_prev = it-1; // ERROR : What I expected; + - operators never existed
it_prev = std::prev(it) // OK
it_next3 = it+3; // ERROR : also,…

siamenock
- 109
- 1
- 1
- 9
4
votes
1 answer
Possible to declare const vector in header file?
Below is some simplified code from a header file in which the free functions are declared but not defined and the vector is both declared and defined.
The cpp file contains the implementation of the free functions.
I was wondering if there was a way…

ksl
- 4,519
- 11
- 65
- 106
4
votes
1 answer
Namespaces and free functions
I have a free function, foo, defined in a namespace N. The header for foo is in the global include search path so anyone can call it by including foo.h.
foo calls another local, free function, foo1, which is defined in foo.cpp.
// foo.h
namespace…

ksl
- 4,519
- 11
- 65
- 106
4
votes
1 answer
Error C2270: Modifiers not allowed on nonmember functions
I'm getting this error when compiling:
error C2270: 'busco' : modifiers not allowed on nonmember functions
I think I understand the reason but I don't know how to fix it, if I take the const out I get a C2662 error.
Here is the code:
template…

moondaisy
- 4,303
- 6
- 41
- 70
4
votes
1 answer
Why does boost recommend using core functions over member functions?
In the documentation for boost.geometry it states
Note: prefer using x = bg::get:<0>(point1);
(as opposed to x = point1.get<0>();)
I have seen this elsewhere in the boost docs. My question is why? Is this a best-practices thing, a performance…

mmdanziger
- 4,466
- 2
- 31
- 47