Questions tagged [non-member-functions]

86 questions
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
  • 911
  • 2
  • 10
  • 21
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,…
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
  • 237,063
  • 85
  • 359
  • 638
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!…
6
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
  • 4,827
  • 5
  • 28
  • 60
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…
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
  • 5,323
  • 3
  • 39
  • 60
4
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,210
  • 2
  • 23
  • 44
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
  • 8
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
  • 3,901
  • 7
  • 50
  • 100
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
  • 3,901
  • 7
  • 50
  • 100
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
  • 3,552
  • 2
  • 24
  • 41
3
votes
2 answers

Writing a subscript non-member function

I'm guessing this just isn't legal in C++, but I thought I'd ask, given a struct that I don't own: struct foo { int x; int y; int z; }; I want to write a non-member subscript operator for it: int& operator [](foo& lhs, const std::size_t…
3
votes
2 answers

Unit test private methods by making them free functions

In the 2017 cppcon videos, I came across a talk by Klaus Iglberger which was entitled "Free Your Functions!". In this talk, the speaker talked about how switching to free functions could easy up the process of testing private methods (See at…
BobMorane
  • 2,070
  • 1
  • 13
  • 29
3
votes
2 answers

How to create a handler in Qt?

I'm working on an application using Qt version 4.8 in C++. I need to use a C function which requires a pointer to a free function: void handler(int dummy) I need to change the displayed values in my GUI depending on what happens inside the…
user2738748
  • 1,056
  • 1
  • 15
  • 32