98 questions
4
votes
1
answer
165
views
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 <vector>
void for_each(auto const& v, auto f) {
for (...
2
votes
1
answer
128
views
Should I convert a class with only methods to free functions in a namespace?
I originally created a class like so:
class A
{
public:
void run(int x);
private:
void run_helper1();
void run_helper2();
void run_helper3();
int a_;
double b_;
bool c_;
};
...
0
votes
1
answer
113
views
What is the minimal way to write a free function to get the member of a class?
(This question popped to my mind after reading this one and its accepted answer.)
Assume a class Foo that you cannot modify, that has a public member bar, but no getter for it.
You might want to write ...
1
vote
1
answer
706
views
how to call member function if it exists, otherwise free function?
I've got various classes:
struct foo final { std::string toString() const { return "foo"; } };
struct bar final { };
std::string toString(const bar&) { return "<bar>"; }
...
2
votes
2
answers
130
views
Call non-member function from inside class, but the nonmember function takes class as input (C++)
As the title suggests, I am wondering if I can call a non-member function (which is contained in the class header file) from inside the class, with the caveat that the nonmember function is using the ...
2
votes
1
answer
704
views
Why is argument-dependent lookup (ADL) choosing class method instead of more fiting free function?
According to cppreference, in Argument-dependent lookups
function names are looked up in the namespaces of their arguments in
addition to the scopes and namespaces considered by the usual
unqualified ...
0
votes
0
answers
27
views
c++ assignment operator overloading of a non-member function [duplicate]
I have a variable class that I am using to make scripting more easier in my program. I am trying to figure out an easy way to do something like the following:
myclass
{
protected:
int _data;
...
}...
0
votes
0
answers
108
views
Is there a way to make Visual Studio find / offer functions that take a particular type as parameter?
Lets say I implement some functions for type Foo. If they are member functions VS will offer them for a foo object but if I implement them as free / non-member functions then I get no help.
foo....
10
votes
1
answer
308
views
unconventional uses of friend in c++
I know the general use cases for the friend keyword with regards to encapsulation but one a couple of occasions, I have needed the friend keyword just to "get the job done". These use cases ...
0
votes
0
answers
81
views
Why are the lexicographical comparison operators assumed as non-member functions for containers?
When having a look at the documentation of std::map, I noticed that the lexicographical comparison operators (between maps) were not counted as member functions. Checked a few other containers (vector,...
1
vote
0
answers
24
views
what operators should be methods , friends, and non-member function? [duplicate]
I have searched for a specific answer for my question and i couldn't find one , so here is my question:
I know that if we have operator+ and operator+= += should be a method and + a non-member ...
0
votes
1
answer
1k
views
How to declare a function returning a class instance, that is used in the same class?
I've tried couple of weeks and searched for days for an answer, but haven't found it. My code is rather large and intertwined, but my problem is with 3 functions/classes, therefore I will only show my ...
0
votes
1
answer
263
views
Sorting by different data members of a class C++
So, I basically learnt class and also Template functions in C++. Suppose I have a record of students in a class with their roll number, name and total marks.
I am using index sorting to sort the ...
-1
votes
1
answer
154
views
Free functions in C++
I want to add v2 to v1. My member function is working, but free function is not. How can I solve this problem, thanks.
When I compile with: clang++ -std=c++2a hw1.cpp -o hw1
and run with: ./hw1
give 5 ...
0
votes
1
answer
376
views
C++Linked list non-member function to reverse print
So I understood how to print a single linked list in reverse order using recursion. I'm having trouble with doing it non member functions.
For example in int print_reverse(IntSLList & list)) ...
1
vote
1
answer
83
views
How to write in-class function from non-class function c++?
I have merge function which is a non-class.
Merge.cpp
template <typename T>
vector<T> merge(vector<T> left, vector<T> right){
vector<T> result;
int left_current = ...
-1
votes
3
answers
12k
views
No operator ">>" matches these operands operand types are: std::istream >> double
For my project I'm trying to create a free function for a complex number class. It is defined in the cpp file. The function is an overloaded input streaming operator but I keep getting the error
No ...
2
votes
2
answers
191
views
Error for default parameter value of non-member template friend function [duplicate]
Why does the following code compile with GCC but not with Clang? Who is right and why?
class TF
{
private:
struct S
{
};
template <...
0
votes
0
answers
218
views
In c++ how do i refer a member function of a class to a non member function which is further being called by another member function of that class? [duplicate]
In c++ how do i refer a member function of a class to a non member function which is further being called by another member function of that class?
This is my code and it return into this error:
[...
2
votes
0
answers
1k
views
GMock EXPECT_CALL failed but test returns OK while mocking C functions
I'm trying to mock the libusb C interface based on the answer here: https://stackoverflow.com/a/41640864/1752391
The tests run just fine if I actually call the expected functions, but when the ...
-1
votes
2
answers
416
views
How do you call a non-member function with a template in c++, where the typename is only in the return?
My goal is to have a non member function use a template for a return value. This is so I can return a float array, a double array, etc. I get a "couldn't deduce template parameter 'T'" error....
0
votes
2
answers
2k
views
C++ "Invalid use of 'this' in non-member function", [duplicate]
Below are the .cpp versions of my character class and its subclasses. I am trying to get the attack() function to work. I made some changes and the current error deals with "invalid use of ‘this’ in ...
0
votes
1
answer
888
views
Accessing non-member functions from another file
I am wondering if it is possible to access non-member functions from another file. That is, a function declared and defined in a .cpp rather than in its header.
I have made a short example to show ...
4
votes
2
answers
2k
views
std::visit a std::variant with overloaded free-function instead of function-object
In C++17 is there a simple way to std::visit a variant with an overloaded free-function or must I use an object with an overloaded call operator?
In other words, is it possible to add something ...
1
vote
2
answers
120
views
Why member function address are so far away from free functions?
Taking this example: https://godbolt.org/z/gHqCSA
#include<iostream>
template<typename Return, typename... Args>
std::ostream& operator <<(std::ostream& os, Return(*p)(Args....
4
votes
2
answers
329
views
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) // ...
0
votes
2
answers
54
views
Excluding member functions and inheritance, what are some of the most common programming patterns for adding functionality to a class?
There're likely no more than 2-4 widely used approaches to this problem.
I have a situation in which there's a common class I use all over the place, and (on occasion) I'd like to give it special ...
3
votes
2
answers
437
views
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 ...
0
votes
2
answers
533
views
why can we access a non_member function from member function in c++
The following code compiles without any warning or error:
#include <iostream>
using namespace std;
class demo_class
{
int x;
float y;
public:
void fun(void);
};
void fun2(void)
{...
1
vote
2
answers
3k
views
How to define an inline free function (non member function) in C++?
In C++, I need to define some inline general functions.
However, when I write the prototype in a header file and the implementation in a.cpp file, I encounter with "LNK2001 unresolved external symbol" ...
1
vote
1
answer
531
views
Helper struct to expose data member to public
so I am trying to construct a class with helper method, namely:
class Type{
int a, b, c;
friend auto helper(auto);
friend auto test_helper(auto);
/* couples test with implement */
...
2
votes
0
answers
83
views
The "free your functions" approach: could IDEs provide hints? [closed]
I have been listening to Klaus Iglberger speech on CppCon 2017 called "Free Your Functions" (you can find it on Youtube here: https://www.youtube.com/watch?v=WLDT1lDOsb4) which exposes better the ...
2
votes
0
answers
930
views
"Rule of thumb" for free function vs member function [closed]
There is a popular guideline (Scott Meyers, Klaus Iglberger, ect) that I recently put more thought into, which is basically prefer non-member (free) functions to member functions. I'm noticing that I ...
1
vote
2
answers
1k
views
How do in include a non-member operator- overloading for a template class in c++?
I am new to c++ and templates is definitely not friendly in syntax. Basically, here are some of the functions i've written, tested, and finished. Just one quick question, i've been trying for hours to ...
3
votes
2
answers
2k
views
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 ...
1
vote
2
answers
156
views
Algorithm find an element in a container with a given value for one of its members
Something that I have to do quite often is finding a member in a collection of elements which has an element with a given value. For example given:
class Person
{
string getName() const {return ...
0
votes
1
answer
206
views
Specify that a non-member function should be called instead of member function
I have a class with a member called f and simultaneously a generic free function called f.
The free function f is meant to called from another member (called g below).
class A{};
int f(A const& ...
0
votes
3
answers
1k
views
C++ static non-member function returning object of a template class
I have a static non-member function which returns a template class object depending on the type of the object.
template< typename T >
class Example
{
....
};
static Example ...
0
votes
3
answers
4k
views
In which file do we put non-member function in C++?
What is the normal practice when it comes to non-member function in C++? Do we put them in main.cpp or header file or class implementation file, or do we make a separate .cpp file for it?
If the ...
2
votes
1
answer
88
views
List of (names of) functions that are specially recognized by C++. (e.g. operator++,begin)
I have just learned C++ for a little bit, and discover some special functions.
Example 1
bool operator<(const B& b1,const B& b2)
bool B::operator<(const B& b2) const
//recognized ...
1
vote
1
answer
571
views
Non member comparison operator for a template class
I have defined a template container Tree<T>, with two member-class iterators : const_iterator and iterator
Now I would like to add non member comparison operators:
template<typename T>
...
-2
votes
2
answers
341
views
What effect does const at the beginning of a non-member function declaration have?
Digging through MSDN, I ran into just another curious line:
// This function returns the constant string "fourth".
const string fourth() { return string("fourth"); }
The full example is buried here: ...
0
votes
1
answer
789
views
CakePHP 3 - call object from other helper
I'm actually working on a Helper for CakePHP3 that include BsHelper and then the BsFormHelper.
Actually everything looks good, no problem with Bootstrap formats.
I try now to create a ckEditor ...
2
votes
1
answer
746
views
IBM Rhapsody: How to use non-member functions in sequence diagrams?
In Rhapsody I have designed an interface which consists of an interface class and a couple of non-member functions. These non-member functions I've put directly into a package which is parallel to the ...
3
votes
2
answers
697
views
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 ...
0
votes
2
answers
232
views
Name, Papers , Books was not declared in this scope non-member function C++
I have an error when I declare the non-member function listOverview();
void listOverview()
{
std::cout << "Overview of books of " << name << std::endl;
for (auto p : books)
...
11
votes
3
answers
1k
views
Does C++ have a free function `size(object)`?
It seems that the way that most people find the size of a string is they just use the my_string.size() and it works fine. Well, I recently did an assignment for class where I did...
if (size(...
4
votes
1
answer
2k
views
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 ...
5
votes
3
answers
1k
views
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 ...
1
vote
1
answer
51
views
In operator lookup no preference is given to members over nonmembers
Stroustrup writes :
Consider a binary operator @. If x is of type X and y is of type Y, x@y is resolved like this:
• If X is a class, look for operator@ as a member of X or as a member of a base of ...