deleted-functions

Is deleting copy and move constructors/assignment operators in base class enough?

情到浓时终转凉″ 提交于 2020-01-14 17:54:27
问题 If I have an abstract base class and I want to make all derived classes noncopyable and nonmovable is it sufficient to declare these special member functions deleted in the base class? I want to ensure that my entire class hierarchy is noncopyable and nonmovable and am wondering if I can get away with not having to declare those 4 special member functions as deleted in every derived class. I saw a SO answer where it seemed to imply that a derived class could explicitly declare a copy or move

Is deleting copy and move constructors/assignment operators in base class enough?

强颜欢笑 提交于 2020-01-14 17:53:29
问题 If I have an abstract base class and I want to make all derived classes noncopyable and nonmovable is it sufficient to declare these special member functions deleted in the base class? I want to ensure that my entire class hierarchy is noncopyable and nonmovable and am wondering if I can get away with not having to declare those 4 special member functions as deleted in every derived class. I saw a SO answer where it seemed to imply that a derived class could explicitly declare a copy or move

Danger with virtual base move assignment operators when they are now allowed to be used?

不问归期 提交于 2019-12-21 18:06:20
问题 This concerns the resolution of C++ Issue http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1402 . Summary: template<typename T> struct wrap { wrap() = default; wrap(wrap&&) = default; wrap(const wrap&) = default; T t; }; struct S { S(){} S(const S&){} S(S&&){} }; typedef wrap<const S> W; // Error, defaulted move constructor of "wrap<const S>" is deleted! W get() { return W(); } (The issue is that we are getting an error for this snippet, even though the compiler could simply use

Specialized template function with deleted “general” case fails to compile with g++ <=4.8.0 and clang++

无人久伴 提交于 2019-12-17 12:49:51
问题 Compiling a project with an older version of g++ (4.8.0, MinGW) I found that this code fails to compile: template<typename T> void foo() = delete; template<> void foo<int>(){} int main() { foo<int>(); return 0; } It seems that g++ doesn't even try to look for explicit specializations if it sees that the base case is deleted. mitalia@mitalia:~/scratch$ /opt/mingw32-dw2/bin/i686-w64-mingw32-g++ -std=c++11 buggy_deleted_template.cpp buggy_deleted_template.cpp: In function 'int main()': buggy

Default move constructor/assignment and deleted copy constructor/assignment

蹲街弑〆低调 提交于 2019-12-17 09:39:53
问题 According to the standard, If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if — X does not have a user-declared copy constructor, — X does not have a user-declared copy assignment operator, — X does not have a user-declared move assignment operator, and — X does not have a user-declared destructor. Now the following fails to compile # include <utility> class Foo { public: Foo() = default; Foo(Foo const &)

Opening stream via function

北战南征 提交于 2019-12-12 12:40:14
问题 I need help with the non-copyable nature of [io](f)stream s. I need to provide a hackish wrapper around fstream s in order to handle files with unicode characters in their filenames on Windows. For this, I devised a wrapper function: bool open_ifstream( istream &stream, const string &filename ) { #ifdef __GLIBCXX__ FILE* result = _wfopen( convert_to_utf16(filename).c_str(), L"r" ); if( result == 0 ) return false; __gnu_cxx::stdio_filebuf<char>* buffer = new __gnu_cxx::stdio_filebuf<char>(

Are there any use cases for a class which is copyable but not movable?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 09:32:50
问题 After reading this recent question by @Mehrdad on which classes should be made non-movable and therefore non-copyable , I starting wondering if there are use cases for a class which can be copied but not moved . Technically, this is possible: struct S { S() { } S(S const& s) { } S(S&&) = delete; }; S foo() { S s1; S s2(s1); // OK (copyable) return s1; // ERROR! (non-movable) } Although S has a copy constructor, it obviously does not model the CopyConstructible concept, because that is in turn

Resolving a compiler error due to an invariant member with a possible deleted default constructor

青春壹個敷衍的年華 提交于 2019-12-11 14:55:47
问题 I have asked a series of questions that all relate to the same source code in this order: experimenting-with-unions-and-bitfields-within-a-structures-and-templates trying-to-flip-the-order-of-bits-in-stdbitset avoiding-ambiguity-in-overload-resolution I've also asked these series of questions over at Code Review that are also related. emulating-virtual-registers-by-experimenting-with-unions-bitfields-structs-and-template-specialization emulating-virtual-registers-part-2 This should give you

Forbids functions with `static_assert`

做~自己de王妃 提交于 2019-12-11 01:06:28
问题 I want to prevent certain functions from being called. Let's ignore the case of calling the function via a function pointer or something, and just concentrate on the case of direct function call. I can do this with = delete . However, the diagnostic issued is not quite informative. I considered using static_assert , with which you can supply a custom diagnostic message. I placed a static_assert(false, ...) statement within the function body, hoping that it fires when the function is called.

Why deleted copy constructor doesn't let to use other constructor with polymorphic type?

随声附和 提交于 2019-12-10 17:38:27
问题 I wonder why this program doesn't compile (the same behavior on msvc, gcc and clang): #include <iostream> using namespace std; struct Action { virtual void action() { cout << "Action::action()\n"; } }; struct ActionDecorator : Action { ActionDecorator(const ActionDecorator&) = delete; ActionDecorator(Action & action) : origAction(action) { } void action() override { decoration(); origAction.action(); } private: void decoration() { cout << "ActionDecorator::decoration()\n"; } Action &