boost move compile error - c++

I'm trying to implement the move constructor outside the class body, but it won't compile correctly
#include <boost/move/move.hpp>
class Test
{
BOOST_COPYABLE_AND_MOVABLE(Test)
public:
Test() {}
Test(const Test & other) { }
Test(BOOST_RV_REF(Test) other);
Test & operator=(BOOST_COPY_ASSIGN_REF(Test) other) { return *this; }
Test & operator=(BOOST_RV_REF(Test) other) { return *this; }
};
Test::Test(BOOST_RV_REF(Test) other) { }
I compiled this code with g++, my g++ version is 4.4.7
$ g++ -c test.cpp
test.cpp:15: error: prototype for 'Test::Test(boost::rv<Test>&)' does not match any in class 'Test'
test.cpp:9: error: candidates are: Test::Test(boost:rv<Test>&)
test.cpp:8: error: Test::Test(const Test&)
test.cpp:7: error: Test::Test()

It also failed with g++ 5.4.0 – flyzero
Must be your boost version.
It works fine with g++ 5.4.1 and Boost 1.64. If not, check the preprocessor output for any include/macro mishaps.

In Linux, ::boost::rv is declared with may_alias attribute. My code compile correctly after removing the may_alias attribute.
#define BOOST_MOVE_ATTRIBUTE_MAY_ALIAS __attribute__((__may_alias__))
template <class T>
class rv
: public ::boost::move_detail::if_c
< ::boost::move_detail::is_class<T>::value
, T
, ::boost::move_detail::nat
>::type
{
rv();
~rv() throw();
rv(rv const&);
void operator=(rv const&);
} BOOST_MOVE_ATTRIBUTE_MAY_ALIAS;

Related

Overload class member function marked const

I am having trouble overloading class member functions that are marked const, while there is no problem when the functions are not marked const. Also the overload itself works fine in pure C++.
The following fails
#include <vector>
#include <pybind11/pybind11.h>
class Foo
{
public:
Foo(){};
std::vector<double> bar(const std::vector<double> &a) const
{
return a;
}
std::vector<int> bar(const std::vector<int> &a) const
{
return a;
}
};
namespace py = pybind11;
PYBIND11_MODULE(example,m)
{
py::class_<Foo>(m, "Foo")
.def("bar", py::overload_cast<const std::vector<double>&>(&Foo::bar));
}
Compiled using:
clang++ -O3 -shared -std=c++14 `python3-config --cflags --ldflags --libs` example.cpp -o example.so -fPIC
Gives error:
...
no matching function for call to object of type 'const detail::overload_cast_impl<const vector<double, allocator<double> > &>'
.def("bar", py::overload_cast<const std::vector<double>&>(&Foo::bar));
...
Whereas the code works when I remove the const mark of the functions.
How should I perform this overload?
There is a special tag for const overloaded methods.
namespace py = pybind11;
PYBIND11_MODULE(example,m)
{
py::class_<Foo>(m, "Foo")
.def("bar", py::overload_cast<const std::vector<double>&>(&Foo::bar, py::const_));
}

Symbol not found in debugger only, when having templated template argument

Given the following code as test.cpp,
Building using clang++ -c test.cpp -o test.o -g; clang++ test.o -g -all_load, setting breakpoint at return a.getValue(); and attempting to p a.getValue() from lldb:
Running llvm 3.8.0 on unix - works perfectly
Running xcode or llvm 8.1.0 on OSX - I get the following error:
error: Couldn't lookup symbols:
__ZNK4Test7MyClassILi2ELi3EE8getValueEv
Two interesting facts:
If I remove the last template argument - all works well
If I build directly without going through the .o file (clang++ test.cpp) = all goes well
Anyone has a clue what is going on, and how can it be fixed?
namespace Test{
template<class T>
class BLA{
public:
T getBlaValue() const{return 3;}
};
template <int N1, int N2, template<class T>class Impl = BLA>
class MyClass {
private:
public:
__attribute__((used))
int getValue() const
{
return 3;
}
};
}
int main()
{
Test::MyClass<2, 3> a;
return a.getValue();
}

Why is GCC warning me about a useless cast when using an inherited constructor?

Consider the following C++ code:
#include <string>
struct A {
A(const std::string& s): s(s) {}
std::string s;
};
struct B: A {
using A::A;
};
int main() {
B b("test");
}
When I compile it via GCC 6.2.1 with the -Wuseless-cast parameter
g++ -std=c++14 -Wuseless-cast test.cpp -o test
it emits the following warning:
test.cpp: In constructor ‘B::B(const string&)’:
test.cpp:9:14: warning: useless cast to type ‘const string& {aka const std::__cxx11::basic_string<char>&}’ [-Wuseless-cast]
using A::A;
^
test.cpp: In function ‘int main()’:
test.cpp:13:15: note: synthesized method ‘B::B(const string&)’ first required here
B b("test");
^
However, when I change the definition of B to
struct B: A {
B(const std::string& s): A(s) {}
};
the warning goes away.
Questions:
Why is the warning emitted?
Why does specifying a constructor for B instead of inheriting it from A fixes the warning?
Your example can be further reduced to:
struct A {
A(const int& i): i(i) {}
int i;
};
struct B: A {
using A::A;
};
int main() {
B b(0);
}
That is an open issue on GCC.
Including <string> isn't apparently required to reproduce it.
Note that the issue is still unconfirmed and it is known to affect at least GCC 6.1 - by looking at your question I would say that it affects also GCC 6.2.
You are probably facing a known GCC bug (PR 70844).

How to declare noexcept if only a member function of an attribute is noexcept?

#include <vector>
class A
{
std::vector<int> vec;
void swap( A & other) noexcept(noexcept(vec.swap(other.vec)))
{
vec.swap(other.vec);
}
};
int main()
{
}
This code compiles under clang(3.4) but not under gcc (4.7.1). Anyone can tell me what I am doing wrong?
EDIT
gcc error message is :
error: invalid use of incomplete type ‘class A’
error: forward declaration of ‘class A’
As a work around, you may use (which works for gcc 4.7.1, gcc 4.8.1 and clang 3.4):
void swap(A& other) noexcept(noexcept(std::declval<decltype(A::vec)&>().swap(std::declval<decltype(A::vec)&>())))
or
void swap(A& other) noexcept(noexcept(vec.swap(vec)))
I think the problem is other.vec...

prohibiting copy constructor with new standard c++11

hey guys I was trying to create a simple class with copy constructor prohibiting.
#include <cstdlib>
//
class my_stack
{
std::size_t last;
int *data;
std::size_t max_elem;
public :
int top(void) const;
void pop(void);
void push(int);
int size(void) const;
bool empty(void) const;
my_stack();
my_stack(int);
~my_stack();
private:
my_stack(const my_stack&) = delete;
void operator=(const my_stack&) = delete;
};
but when i compile it warning appeared
defaulted and deleted functions only available with -std=c++0x or -std=gnu++0x [enabled by default]
I use g++ compiler. How can I avoid this warning, may be I should use some compiler parameters.
I fixed it.
problem was I used
g++ *.cpp -c -std=c++0x
instead
g++ -std=c++0x *.cpp -c