prohibiting copy constructor with new standard c++11 - c++

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

Related

static assertion failed: comparison object must be invocable as const when using my custom comparison function in set container [duplicate]

Sample code:
#include <string>
#include <set>
using namespace std;
class x
{
private:
int i;
public:
int get_i() const { return i; }
};
struct x_cmp
{
bool operator()(x const & m1, x const & m2)
#if _MSC_VER
const
#endif
{
return m1.get_i() > m2.get_i();
}
};
std::set<x, x_cmp> members;
void add_member(x const & member)
{
members.insert(member);
}
Invocations:
$ g++ -c -std=c++14 -pedantic -Wall -Wextra
<nothing>
$ clang++ -c -std=c++14 -pedantic -Wall -Wextra
<nothing>
$ icc -c -std=c++14 -pedantic -Wall -Wextra
<nothing>
$ cl /c /std:c++14 /Za
<nothing>
Question: why msvc requires const while others don't? Or why others don't require const?
This is LWG2542. In C++14, the wording for the comparison operator said "possibly const", which was interpreted by GCC and Clang as meaning that the comparison operator was not required to be const qualified. MSVC always required it.
This is a wording defect, as comparison operators for associative containers should be const qualified. This was changed in C++17 to require the comparator to be const. This is a breaking change, so valid (though broken) C++14 code may fail to compile in C++17.

Statically initializing constexpr std::array of objects containing function pointers

I am trying to statically initialize a constexpr std::array of objects containing function pointers with the following code:
#include <array>
using TVoidVoid = void(*)(void);
class State{
public:
constexpr State(TVoidVoid function) : function_{function}{}
private:
TVoidVoid function_;
};
void OnEvent1(){}
void OnEvent2(){}
constexpr std::array<State, 10> states = {{OnEvent1}, {OnEvent2}};
int main(){}
I am compiling with:
g++ -Wall -Wextra -Wshadow -Weffc++ -Wstrict-aliasing -ansi -pedantic -Werror -std=c++14 main.cpp
I have trouble understanding the compiling error I'm getting:
main.cpp:14:69: error: too many initializers for ‘const std::array<State, 10>’
constexpr std::array<State, 10> states = {{OnEvent1}, {OnEvent2}}
The compiler is g++ (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0.
What could be the problem here? Many thanks in advance!
The error message could be better. But what's tripping the initialization is in fact that you don't have enough braces. Recall that a std::array is an aggregate wrapping a raw array. So you need to initialize like this:
constexpr std::array<State, 10> states = {{ {OnEvent1}, {OnEvent2} }};
Otherwise, the somewhat inaccurate brace ellision detection algorithm assumes {OnEvent1} is to initialize the internal array, and the second clause is redundant.
Now you just need to provide a default c'tor for State, or adjust the array size.
You need a default constructor (for the last 8)
#include <array>
using TVoidVoid = void(*)(void);
class State{
public:
// This static is equivalent to a TVoidVoid
// used by the default constructor
static void DefFunct() {}
constexpr State(TVoidVoid function) : function_{function}{}
// We create a default constructor for the
// empty elemnts of the array with our function
constexpr State() : function_(DefFunct) {}
private:
TVoidVoid function_;
};
void OnEvent1(){}
void OnEvent2(){}
constexpr std::array<State, 10> states = {OnEvent1, OnEvent2};
int main(){}

Workaround for Clang "exception specification is not available until end of class definition" bug

Given this code:
template <typename>
struct Check { constexpr static bool value = true; };
struct A {
A() noexcept(Check<int>::value) = default;
};
Compiling with various versions of GNU g++ works fine, but it always fails with clang++ 5.0.1 (both with libstdc++ and libc++):
$ g++-4.9.4 test.cpp -c -o test -std=c++11 -Wall -Wextra
$ g++-5.4.0 test.cpp -c -o test -std=c++11 -Wall -Wextra
$ g++-6.4.0 test.cpp -c -o test -std=c++11 -Wall -Wextra
$ g++-7.2.0 test.cpp -c -o test -std=c++11 -Wall -Wextra
$ clang++ test.cpp -c -o test -std=c++11 -Wall -Wextra -Weverything
test.cpp:2:16: warning: 'constexpr' specifier is incompatible with C++98 [-Wc++98-compat]
struct Check { constexpr static bool value = true; };
^
test.cpp:5:39: warning: defaulted function definitions are incompatible with C++98 [-Wc++98-compat]
A() noexcept(Check<int>::value) = default;
^
test.cpp:5:9: warning: noexcept specifications are incompatible with C++98 [-Wc++98-compat]
A() noexcept(Check<int>::value) = default;
^
test.cpp:5:5: error: exception specification is not available until end of class definition
A() noexcept(Check<int>::value) = default;
^
test.cpp:5:18: note: in instantiation of template class 'Check<int>' requested here
A() noexcept(Check<int>::value) = default;
^
3 warnings and 1 error generated.
$ clang++ test.cpp -c -o test -std=c++11 -Wall -Wextra
test.cpp:5:5: error: exception specification is not available until end of class definition
A() noexcept(Check<int>::value) = default;
^
test.cpp:5:18: note: in instantiation of template class 'Check<int>' requested here
A() noexcept(Check<int>::value) = default;
^
1 error generated.
On Compiler Explorer this also seems to work for all versions of GCC newer 4.7.0 (including trunk), but fails for all Clang versions except for Clang 3.4.0, 3.5.0, 3.5.1 and trunk. So this seems like Clang bug.
Is it possible to work around this bug? How? Is this bug already tracked somewhere?
EDIT:
I tracked this bug down to Clang PR23383. As of now there is no notice about this being fixed in Clang although it seems to work with Clang trunk in Compiler Explorer.
This might be related to PR30860 (and C++ DR1330 as described in Richard Smith's comment on PR30860). It seems that the issue has something to do about when the contents of the noexcept() are parsed.
And so it turns out that one obvious workaround is to try to force the compiler to parse these contents elsewhere. One possible solution would be to provide the contents noexcept() as a constexpr member constant:
template <typename>
struct Check { constexpr static bool value = true; };
struct A {
A() noexcept(workaround) = default;
private: /* Work around Clang PR23383: */
static constexpr bool workaround = Check<int>::value;
};
Unfortunately, this does not work in all cases, e.g. such as this:
#include <type_traits>
struct Base { virtual ~Base() noexcept; };
struct OuterClass {
class InnerDerived: public Base {
private:
static constexpr auto const workaround =
std::is_nothrow_default_constructible<Base>::value;
public:
InnerDerived() noexcept(workaround);
};
class InnerDerived2: public InnerDerived {
private:
static constexpr auto const workaround2 =
std::is_nothrow_default_constructible<InnerDerived>::value;
public:
InnerDerived2() noexcept(workaround2);
};
};
The latter yields the error even when using Clang trunk in Compiler Explorer. Any ideas why?

boost move compile error

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;

Is it legal to use functions returning a temporary in initialiser lists

I have the following constructor of an object
Segment::Segment(QPointF const& start, QPointF const& end):
mOrigin(toVector3df(start)),mEnd(toVector3df(end)){
}
mOrigin is of type Vector3df and the function toVector3df(QPointF const&) returns a temporary Vector3df. So far so good. The code compiles fine and works like a charm under linux, gcc 4.4.3. most warnings activated.
Now I wanted to cross-compile the same code for a Nokia Smartphone (Meamo Fremantle)
and all of a sudden I get very weird compiler warnings:
include/vector3d.h: In constructor 'Segment::Segment(const QPointF&, const QPointF&)':
include/vector3d.h:64: warning: 'this.902' is used uninitialized in this function
include/vector3d.h:64: note: 'this.902' was declared here
First: Of course there is no real variable called this.902 inside 'Vecto3df' so my first question would be: "Has anyone seen some warning like this ?" Further there is nothing wrong with Vector3df constructors, they are very simple and toVector3df(QPointF const&) is a one liner non-member template function that works perfect in other parts of the code.
Vector3df inherits from a template that only defines non-member functions, no variables no, virtual functions.
Second, when I change the above code to the following
Segment::Segment(QPointF const& start, QPointF const& end):
mOrigin(),mEnd(){
mOrigin = toVector3df(start);
mEnd = toVector3df(end);
}
The code works fine without any warnings.
So what am I missing here ? Has anybody an idea what the origin of the warnings could be. Am I violating some doctrine I'm unaware of. Is the fremantle compiler (Maemo 5, Qt 4.6.2) more severe or buggy ?
Thanks in advance, Martin
Edit:
Here is a minimal example, sorry for the length :-P
#include <iostream>
#include <sstream>
#include <QPoint>
template<typename T> class IoEnabled {};
template<typename T>
class Vector3d: public IoEnabled<Vector3d<T> > {
private:
T mX; T mY; T mZ;
public:
Vector3d(T const& x, T const& y, T const& z=0.0) : mX(x), mY(y), mZ(z) {}
};
typedef Vector3d<float> Vector3df;
template<class T>
Vector3df toVector3df(T const& p){
return Vector3df(p.x(),p.y(),0.0);
}
class Segment {
private:
Vector3df mOrigin; Vector3df mEnd;
public:
Segment(QPointF const& start, QPointF const& end):
mOrigin(toVector3df(start)),mEnd(toVector3df(end)){
//if toVector3df(...) is moved from the initializer to the body it works
}
};
int main(int argc, char **argv) {
(void) argc; (void) argv;
Segment temp(QPointF(1,2),QPointF(3,4));
return 0;
}
Compiler call:
g++ -c -pipe -Werror -Wall -Wextra -Wunused -Wundef -Wpointer-arith -Wcast-align -Wwrite-strings -Wredundant-decls -O3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -D_REENTRANT -Wall -W -DQT_GL_NO_SCISSOR_TEST -DQT_DEFAULT_TEXTURE_GLYPH_CACHE_WIDTH=1024 -DMAEMO -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/opt/QtSDK/Maemo/4.6.2/sysroots/fremantle-arm-sysroot-20.2010.36-2-slim/usr/share/qt4/mkspecs/linux-g++-maemo5 -I. -I/opt/QtSDK/Maemo/4.6.2/sysroots/fremantle-arm-sysroot-20.2010.36-2-slim/usr/include/QtCore -I/opt/QtSDK/Maemo/4.6.2/sysroots/fremantle-arm-sysroot-20.2010.36-2-slim/usr/include/QtGui -I/opt/QtSDK/Maemo/4.6.2/sysroots/fremantle-arm-sysroot-20.2010.36-2-slim/usr/include -Isrc -Irelease/moc -o release/obj/main.o src/main.cpp
The Template inheritance seems to be crucial, if the Vector3d does not inherit everything works fine.
There is nothing wrong in using functions returning a temporary in member initializer lists.
Even the order in which the members will be inialized is well defined in the standard.