C++: Where is my rvalue? - c++

Please consider the following code:
#include <iostream>
template <class T>
class value_wrapper
{
public:
value_wrapper(T& pv) : v(pv) { std::cout<< "CONS.REF:" << pv << " AT:" << (void*)this << std::endl; }
value_wrapper(T&& pv) : v(pv) { std::cout<< "CONS.UNIREF:" << pv << " AT:" << (void*)this << std::endl; }
virtual ~value_wrapper() { std::cout<< "DEST:" << v << " AT:" << (void*)this << std::endl; }
value_wrapper(const value_wrapper& ov) : v(ov.v) { std::cout<< "CONS.COPY.REF:" << v << " AT:" << (void*)this << std::endl; }
value_wrapper(value_wrapper&& ov) : v(ov.v) { std::cout<< "CONS.COPY.UNIREF:" << v << " AT:" << (void*)this << std::endl; }
value_wrapper<T>& operator = (value_wrapper<T>&& ov)
{
std::cout<< "ASSI.UNIREF: OF " << v << " AT:" << (void*)this << " TO:" << ov.v << " AT:" <<(void*)&ov << " ADR.VAL:" << (void*)(&ov.v)<< std::endl;
v = ov.v;
return *this;
}
private:
template <typename V> friend value_wrapper<V> operator - (const value_wrapper<V> v1, const V& v2);
T& v;
};
template<typename T>
value_wrapper<T> operator - (const value_wrapper<T> v1, const T& v2)
{
T f = v1.v - v2;
value_wrapper<T> res(f);
std::cout << "MINUS: RESULT:" << res.v << " AT:" << (void*)&res << " ADR.VAL:" << &res.v << std::endl;
return res;
}
template<typename X>
value_wrapper<X> _ (X& a)
{
return value_wrapper<X>(a);
}
int main()
{
int a = 5;
std::cout << "BEFOR:" << a<< std::endl;
_(a) = _(a) - 1;
std::cout << "AFTER:" << a<< std::endl;
return 0;
}
And their online presence:
(The bad one) http://cpp.sh/7yav
(The good one) http://coliru.stacked-crooked.com/a/ea7363eaba68a336
While the first one outputs:
BEFOR:5
CONS.REF:5 AT:0x761cebd52310
CONS.REF:4 AT:0x761cebd52320
MINUS: RESULT:4 AT:0x761cebd52320 ADR.VAL:0x761cebd522cc
CONS.REF:5 AT:0x761cebd52300
ASSI.UNIREF: OF 5 AT:0x761cebd52300 TO:0 AT:0x761cebd52320 ADR.VAL:0x761cebd522cc
DEST:0 AT:0x761cebd52300
DEST:27644 AT:0x761cebd52320
DEST:0 AT:0x761cebd52310
AFTER:0
and the second one outputs:
g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
BEFOR:5
CONS.REF:5 AT:0x7fffc51a5710
CONS.REF:4 AT:0x7fffc51a5720
MINUS: RESULT:4 AT:0x7fffc51a5720 ADR.VAL:0x7fffc51a56fc
CONS.REF:5 AT:0x7fffc51a5700
ASSI.UNIREF: OF 5 AT:0x7fffc51a5700 TO:4 AT:0x7fffc51a5720 ADR.VAL:0x7fffc51a56fc
DEST:4 AT:0x7fffc51a5700
DEST:4 AT:0x7fffc51a5720
DEST:4 AT:0x7fffc51a5710
AFTER:4
So, here comes the question:
what happened to the rvalue from the moment it had the correct value:
MINUS: RESULT:4 AT:0x761cebd52320 ADR.VAL:0x761cebd522cc
Till the correct value disappeared from the same address:
ASSI.UNIREF: OF 5 AT:0x761cebd52300 TO:0 AT:0x761cebd52320 ADR.VAL:0x761cebd522cc
It seems, that I use the same compiler (g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4) as the one from cpp.sh because I get the same results...
Is this a compiler bug, or just my misunderstanding of how a standard rvalue should work?
EDIT:
I Expected the line:
ASSI.UNIREF: OF 5 AT:0x761cebd52300 TO:0 AT:0x761cebd52320 ADR.VAL:0x761cebd522cc
to be:
ASSI.UNIREF: OF 5 AT:0x761cebd52300 TO:4 AT:0x761cebd52320 ADR.VAL:0x761cebd522cc
ie: the value 0 to be 4 just as in the other printout, becaue that has to be the correct value, not 0.

I think you have an undefined behaviour here:
template<typename T>
value_wrapper<T> operator - (const value_wrapper<T> v1, const T& v2)
{
T f = v1.v - v2;
value_wrapper<T> res(f);
return res;
}
See? You are returning a value_wrapper<T> that encapsulates a reference to T, and that reference refers to f. Buf f is a local variable, so you return a dangling reference, and that is UB.

Related

Error in calculating general lpNorm with Eigen

I am trying to calculate the lpNorm of a vector with the Eigen library.
As it can be seen in the example below, with explicit values, such as v.lpNorm<1>(), it works. But it doesn't work inside the loop, with lpNorm<p>()
How can I fix this?
#include <iostream>
#include <Eigen/Dense>
using Eigen::VectorXd;
int main()
{
int sizev = 3;
int p;
Eigen::VectorXd v(sizev);
v(0) = 3.;
v(1) = 2.;
v(2) = 1.;
// test 1, passes
std::cout << "||v||_" << 1 << " = " << v.lpNorm<1>() << std::endl;
std::cout << "||v||_" << 2 << " = " << v.lpNorm<2>() << std::endl;
std::cout << "||v||_" << 3 << " = " << v.lpNorm<3>() << std::endl;
std::cout << "||v||_" << 4 << " = " << v.lpNorm<4>() << std::endl;
std::cout << "||v||_inf = " << v.lpNorm<Eigen::Infinity>() << std::endl;
// test 2, fails
for (int p=1; p<5; p++)
{
std::cout << "||v||_" << p << " = " << v.lpNorm<p>() << std::endl;
}
return 0;
}
On compilation, I am getting the error
error: no matching member function for call to 'lpNorm'
std::cout << "||v||_" << p << " = " << v.lpNorm<p>() << std::endl;
~~^~~~~~~~~
note: candidate template ignored: invalid explicitly-specified argument for template parameter 'p'
template<int p> EIGEN_DEVICE_FUNC RealScalar lpNorm() const;
^
1 error generated.
You cannot use variable integer as a template argument, it needs to be a compile time constant, such as
constexpr int p = 3;
v.lpNorm<p>();
However, you can still have a kind of compile-time loop using e.g. std::integer_sequence. I modified a bit the example from documentation to call a function:
template<typename T, T... ints>
void exec_constexpr_loop(std::integer_sequence<T, ints...> int_seq, Eigen::Ref<Eigen::VectorXd> v)
{
((v.lpNorm<ints>()), ...);
}
exec_constexpr_loop(std::integer_sequence<int, 1, 2, 3>{}, v);
Live demo with dummy function, works since C++17.

Proper handling of member function pointers

I wrote a generic class for handling and executing a function pointer. This is a simplified equivalent of std::function and std::bind. To handle member functions I use cast to internal EventHandler::Class type. Question: is it ok to cast it that way? Will it work in all cases when invoking handled function?
template <typename ReturnType, typename... Arguments>
class EventHandler
{
class Class {};
ReturnType (Class::*memberFunction)(Arguments...) = nullptr;
union {
Class *owner;
ReturnType(*function)(Arguments...) = nullptr;
};
public:
EventHandler() = default;
EventHandler(EventHandler &&) = default;
EventHandler(const EventHandler &) = default;
EventHandler &operator=(EventHandler &&) = default;
EventHandler &operator=(const EventHandler &) = default;
EventHandler(ReturnType (*function)(Arguments...)) :
function(function)
{
}
template <typename Owner>
EventHandler(Owner *owner, ReturnType (Owner::*memberFunction)(Arguments...)) :
memberFunction((ReturnType (Class::*)(Arguments...)) memberFunction),
owner((Class *) owner)
{
}
template <typename Owner>
EventHandler(const Owner *owner, ReturnType (Owner::*memberFunction)(Arguments...) const) :
memberFunction((ReturnType (Class::*)(Arguments...)) memberFunction),
owner((Class *) owner)
{
}
ReturnType operator()(Arguments... arguments)
{
return memberFunction ?
(owner ? (owner->*memberFunction)(arguments...) : ReturnType()) :
(function ? function(arguments...) : ReturnType());
}
};
The implementation provides handle for a global function, a member function and a const member function. Obviously there is volatile and const volatile that is not show here for clarity.
EDIT
All the code below is just a representation of all of kinds of supported functions.
class Object
{
public:
double y = 1000;
Object() = default;
Object(double y) : y(y) {}
static void s1(void) { std::cout << "s1()" << std::endl; }
static void s2(int a) { std::cout << "s2(a:" << 10 + a << ")" << std::endl; }
static void s3(int a, float b) { std::cout << "s3(a:" << 10 + a << ", b:" << 10 + b << ")" << std::endl; }
static int s4(void) { std::cout << "s4(): "; return 10 + 4; }
static Object s5(int a) { std::cout << "s5(a:" << 10 + a << "): "; return Object(10 + 5.1); }
static float s6(int a, Object b) { std::cout << "s6(a:" << 10 + a << ", b:" << 10 + b.y << "); "; return 10 + 6.2f; }
void m1(void) { std::cout << "m1()" << std::endl; }
void m2(int a) { std::cout << "m2(a:" << y + a << ")" << std::endl; }
void m3(int a, float b) { std::cout << "m3(a:" << y + a << ", b:" << y + b << ")" << std::endl; }
int m4(void) { std::cout << "m4(): "; return ((int) y) + 4; }
Object m5(int a) { std::cout << "m5(a:" << y + a << "): "; return Object(y + 5.1); }
float m6(int a, Object b) { std::cout << "m6(a:" << y + a << ", b:" << y + b.y << "); "; return ((int) y) + 6.2f; }
void c1(void) const { std::cout << "c1()" << std::endl; }
void c2(int a) const { std::cout << "c2(a:" << y + a << ")" << std::endl; }
void c3(int a, float b) const { std::cout << "c3(a:" << y + a << ", b:" << y + b << ")" << std::endl; }
int c4(void) const { std::cout << "c4(): "; return ((int) y) + 4; }
Object c5(int a) const { std::cout << "c5(a:" << y + a << "): "; return Object(y + 5.1); }
float c6(int a, Object b) const { std::cout << "c6(a:" << y + a << ", b:" << y + b.y << "); "; return ((int) y) + 6.2f; }
};
void f1(void) { std::cout << "f1()" << std::endl; }
void f2(int a) { std::cout << "f2(a:" << a << ")" << std::endl; }
void f3(int a, float b) { std::cout << "f3(a:" << a << ", b:" << b << ")" << std::endl; }
int f4(void) { std::cout << "f4(): "; return 4; }
Object f5(int a) { std::cout << "f5(a:" << a << "): "; return Object(5.1); }
float f6(int a, Object b) { std::cout << "f6(a:" << a << ", b:" << b.y << "); "; return 6.2f; }
Here is the usage example for all of the above functions
int main()
{
std::cout << "=== Global functions" << std::endl;
EventHandler ef1(f1); ef1();
EventHandler ef2(f2); ef2(2);
EventHandler ef3(f3); ef3(3, 3.1f);
EventHandler ef4(f4); std::cout << ef4() << std::endl;
EventHandler ef5(f5); std::cout << ef5(5).y << std::endl;
EventHandler ef6(f6); std::cout << ef6(6, Object(6.1)) << std::endl;
std::cout << std::endl;
std::cout << "=== Member static functions" << std::endl;
EventHandler es1(Object::s1); es1();
EventHandler es2(Object::s2); es2(2);
EventHandler es3(Object::s3); es3(3, 3.1f);
EventHandler es4(Object::s4); std::cout << es4() << std::endl;
EventHandler es5(Object::s5); std::cout << es5(5).y << std::endl;
EventHandler es6(Object::s6); std::cout << es6(6, Object(6.1)) << std::endl;
std::cout << std::endl;
std::cout << "=== Member functions" << std::endl;
Object object(20);
EventHandler em1(&object, &Object::m1); em1();
EventHandler em2(&object, &Object::m2); em2(2);
EventHandler em3(&object, &Object::m3); em3(3, 3.1f);
EventHandler em4(&object, &Object::m4); std::cout << em4() << std::endl;
EventHandler em5(&object, &Object::m5); std::cout << em5(5).y << std::endl;
EventHandler em6(&object, &Object::m6); std::cout << em6(6, Object(6.1)) << std::endl;
std::cout << std::endl;
std::cout << "=== Member const functions" << std::endl;
const Object constObject(30);
EventHandler ec1(&constObject, &Object::c1); ec1();
EventHandler ec2(&constObject, &Object::c2); ec2(2);
EventHandler ec3(&constObject, &Object::c3); ec3(3, 3.1f);
EventHandler ec4(&constObject, &Object::c4); std::cout << ec4() << std::endl;
EventHandler ec5(&constObject, &Object::c5); std::cout << ec5(5).y << std::endl;
EventHandler ec6(&constObject, &Object::c6); std::cout << ec6(6, Object(6.1)) << std::endl;
system("pause");
return 0;
}
Finally - to the point - here an example that shows how much easier in use is the EventHandler I prepared when compared to std::function interface. And actually the reason of such approach.
EventHandler<float, int, Object> example;
example = f6;
example(7, Object(7.1));
example = EventHandler(&object, &Object::m6);;
example(8, Object(8.1));
It’s undefined behavior to call a function through a function pointer(-to-member) of a different type. (Some practical reasons for this rule are that the object’s address might need to be adjusted to call a member function of a base class or that a vtable might be involved.) You can use type erasure to allow calling member functions on objects of different types (which is what std::bind does), or you can (restrict to member functions and) add the class type as a template parameter.
Of course, the usual answer is to just use std::function with a lambda that captures the object in question and calls whatever member function. You can also take the C approach and define various functions with a void* parameter that cast that parameter to a known class type and call the desired member function.

Const reference parameter in lambda function

#include <iostream>
#include <functional>
using Callback = std::function<void(const int)>;
int main() {
Callback testCall = [](const int &num) {
std::cout << "callback: " << num << " - " << &num << std::endl;
};
int num = 42;
testCall(num);
std::cout << "main: " << num << " - " << &num << std::endl;
}
Possible output:
callback: 42 - 000000B19197F618
main: 42 - 000000B19197F694
As you can see, even if i assign a lambda function which takes the parameter by reference it still uses a copy.
Is that correct?
If yes, why does it still compile? Why is there at least not a warning about the discrepancy between the Callback declaration parameters and the assigned lambda. (const int &num vs const int num)
When not usingconst it does not compile.
PS. if you find a better title, feel free to edit.
This is because testCall is a functor object that catch its parameter by copy and then call the lambda on it.
Try:
Callback f = [](const int &num) {
std::cout << "callback: " << num << " - " << &num << std::endl;
};
int main() {
int num = 999;
std::cout << "callback: " << num << " - " << &num << std::endl;
f(num);
[](const int &num) {
std::cout << "callback: " << num << " - " << &num << std::endl;
}(num);
}
you will see something like:
callback: 999 - 0x7ffeed60a9bc
callback: 999 - 0x7ffeed60a994
callback: 999 - 0x7ffeed60a9bc
which means that callBack is not the function by itself but an indirection to the function. And there is no problem regarding types...
Answer to this may helps you to understand what happens under the hood: How std::function works

Why is the destructor of this custom allocator being called twice in GCC/MSVS's stdlib

I've been trying to write a simple allocator, I've written a minimal one that just logs its calls.
When trying to use it in some simple std::vector operations - at least on GCC and Visual Studio - the logged behaviour looks intuitive except that the destructor appears to be called before all of the allocations are requested, as well as at the end. On clang everything works as one would expect, so I'm not sure if this is just a compiler issue.
Assuming it's not a compiler error, what's missing from this allocator; or is my understanding of how the allocator is being called wrong and it's just fine?
I have the code below as a live demo here
#include <ios>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using std::size_t;
struct Indexer
{
static size_t nextId, objectsAlive;
};
size_t Indexer::nextId, Indexer::objectsAlive;
template<typename T>
class DebugAllocator : protected Indexer
{
static std::string formatPointer(const void* p)
{
std::ostringstream s;
s << "[93m0x" << std::hex << std::uppercase << uintptr_t(p) << "[0m";
return s.str();
}
static std::string formatFunctionName(const char* functionName)
{
return "[96m" + std::string(functionName) + "[0m";
}
static std::string indentation()
{
return std::string((objectsAlive + 1) * 4, ' ');
}
public:
using value_type = T;
using pointer = value_type*;
using size_type = std::make_unsigned_t<typename std::pointer_traits<pointer>::difference_type>;
size_t id;
DebugAllocator() noexcept
: id(nextId++)
{
std::cerr << indentation() << "DebugAllocator::" << formatFunctionName(__func__) << '(' << id << ")\n";
++objectsAlive;
}
template<typename T_rhs>
DebugAllocator(const DebugAllocator<T_rhs>& rhs) noexcept
: id(nextId++)
{
std::cerr << indentation() << "DebugAllocator::" << formatFunctionName(__func__) << '(' << id << ", " << rhs.id << ")\n";
++objectsAlive;
}
template<typename T_rhs>
DebugAllocator& operator=(const DebugAllocator<T_rhs>& rhs) noexcept
{
std::cerr << indentation() << id << " = DebugAllocator::" << formatFunctionName(__func__) << '(' << id << ", " << rhs.id << ")\n";
}
~DebugAllocator() noexcept
{
--objectsAlive;
std::cerr << indentation() << "DebugAllocator::" << formatFunctionName(__func__) << '(' << id << ")\n";
}
pointer allocate(size_type n) const
{
value_type* const p((value_type*) new char[sizeof(value_type) * n]);
std::cerr << indentation() << formatPointer(p) << " = DebugAllocator::" << formatFunctionName(__func__) << '(' << id << ", " << n << ")\n";
return p;
}
void deallocate(pointer p, size_type n) const noexcept
{
std::cerr << indentation() << "DebugAllocator::" << formatFunctionName(__func__) << '(' << id << ", " << formatPointer(p) << ", " << n << ")\n";
delete[] (value_type*) p;
}
bool operator==(const DebugAllocator& rhs) const noexcept
{
std::cerr << indentation() << std::boolalpha << true << " = DebugAllocator::" << formatFunctionName(__func__) << '(' << id << ", " << rhs.id << ")\n";
return true;
}
bool operator!=(const DebugAllocator& rhs) const noexcept
{
std::cerr << indentation() << std::boolalpha << false << " = DebugAllocator::" << formatFunctionName(__func__) << '(' << id << ", " << rhs.id << ")\n";
return false;
}
};
int main()
{
std::vector<int, DebugAllocator<int>> v{3};
v.push_back(1);
v.push_back(2);
v.emplace_back(1);
v.insert(std::begin(v) + 2, 4);
v.erase(std::begin(v) + 3);
std::string separator;
for (int& x : v)
{
std::cerr << separator << std::move(x);
separator = ", ";
}
std::cerr << '\n';
}
GCC / MSVS log:
DebugAllocator::DebugAllocator(0)
0xF86C50 = DebugAllocator::allocate(0, 1)
DebugAllocator::~DebugAllocator(0)
0xF86CA0 = DebugAllocator::allocate(0, 2)
DebugAllocator::deallocate(0, 0xF86C50, 1)
0xF86C50 = DebugAllocator::allocate(0, 4)
DebugAllocator::deallocate(0, 0xF86CA0, 2)
0xF86C20 = DebugAllocator::allocate(0, 8)
DebugAllocator::deallocate(0, 0xF86C50, 4)
3, 1, 4, 1
DebugAllocator::deallocate(0, 0xF86C20, 8)
DebugAllocator::~DebugAllocator(0)
clang log:
DebugAllocator::DebugAllocator(0)
0xD886F0 = DebugAllocator::allocate(0, 1)
0xD88710 = DebugAllocator::allocate(0, 2)
DebugAllocator::deallocate(0, 0xD886F0, 1)
0xD886F0 = DebugAllocator::allocate(0, 4)
DebugAllocator::deallocate(0, 0xD88710, 2)
0xD88730 = DebugAllocator::allocate(0, 8)
DebugAllocator::deallocate(0, 0xD886F0, 4)
3, 1, 4, 1
DebugAllocator::deallocate(0, 0xD88730, 8)
DebugAllocator::~DebugAllocator(0)
Apparently
template<typename T_rhs>
DebugAllocator(const DebugAllocator<T_rhs>& rhs)
does not count as a copy constructor. So a compiler-generated copy-constructor is called that you didn't observe.

Why b.isEm() prints different things on different lines?

Why b.isEm() prints different things on different lines when I have not changed anything after the last call of b.isEm()?
#include <iostream>
#include <string>
template <class T>
class Box
{
bool m_i;
T m_c;
public:
bool isEm() const;
void put(const T& c);
T get();
};
template <class T>
bool Box<T>::isEm() const
{
return m_i;
}
template <class T>
void Box<T>::put(const T& c)
{
m_i = false;
m_c = c;
}
template <class T>
T Box<T>::get()
{
m_i = true;
return T();
}
int main()
{
Box<int> b;
b.put(10);
std::cout << b.get() << " " << b.isEm() << std::endl;
std::cout << b.isEm() << std::endl;
}
The order of evaluation of function arguments in C++ is unspecified.
std::cout << b.get() << " " << b.isEm() << std::endl;
std::cout << b.isEm() << std::endl;
Since b.get() has side effects, I suggest you call it separately...
auto g = b.get();
std::cout << g << " " << b.isEm() << std::endl;
std::cout << b.isEm() << std::endl;
Note: std::cout << .... << ... << is a function call with the arguments ...