Avoid calling default, move and copy constructor
up vote
5
down vote
favorite
I have following example (extension to Avoid calling move constructor)
#include <cstdint>
class Interface
{
public:
Interface() = default;
virtual ~Interface() = default;
Interface(const Interface&) = delete;
Interface(Interface&&) = delete;
const Interface& operator=(const Interface&) = delete;
Interface& operator=(Interface&&) = delete;
};
class FooC : public Interface
{
public:
FooC(uint16_t iPort, uint16_t iPin)
: PORT(iPort)
, PIN(iPin)
{
};
FooC() = delete;
~FooC() override = default;
FooC(const FooC&) = delete;
FooC(FooC&&) = delete;
const FooC& operator=(const FooC&) = delete;
FooC& operator=(FooC&&) = delete;
private:
const uint16_t PORT;
const uint16_t PIN;
};
class FactoryC
{
public:
FactoryC()
: mFoo{
{1, 2},
{3, 4}
}
{
};
~FactoryC() = default;
FactoryC(const FactoryC&) = delete;
FactoryC(FactoryC&&) = delete;
const FactoryC& operator=(const FactoryC&) = delete;
FactoryC& operator=(FactoryC&&) = delete;
private:
FooC mFoo[2];
};
int main()
{
FactoryC factory{};
}
and I don't want to call the default, move and copy constructor. Due to that I deleted the functions. Unfortunately this results in following error (compiled with C++17)
<source>: In constructor 'FactoryC::FactoryC()':
<source>:42:4: error: use of deleted function 'FooC::FooC(FooC&&)'
}
^
<source>:26:4: note: declared here
FooC(FooC&&) = delete;
^~~~
Compiler returned: 1
Is it possible to force in this example the calling of constructor with the parameters and still delete the default, move and copy constructor of FooC?
c++ c++11 gcc c++17
add a comment |
up vote
5
down vote
favorite
I have following example (extension to Avoid calling move constructor)
#include <cstdint>
class Interface
{
public:
Interface() = default;
virtual ~Interface() = default;
Interface(const Interface&) = delete;
Interface(Interface&&) = delete;
const Interface& operator=(const Interface&) = delete;
Interface& operator=(Interface&&) = delete;
};
class FooC : public Interface
{
public:
FooC(uint16_t iPort, uint16_t iPin)
: PORT(iPort)
, PIN(iPin)
{
};
FooC() = delete;
~FooC() override = default;
FooC(const FooC&) = delete;
FooC(FooC&&) = delete;
const FooC& operator=(const FooC&) = delete;
FooC& operator=(FooC&&) = delete;
private:
const uint16_t PORT;
const uint16_t PIN;
};
class FactoryC
{
public:
FactoryC()
: mFoo{
{1, 2},
{3, 4}
}
{
};
~FactoryC() = default;
FactoryC(const FactoryC&) = delete;
FactoryC(FactoryC&&) = delete;
const FactoryC& operator=(const FactoryC&) = delete;
FactoryC& operator=(FactoryC&&) = delete;
private:
FooC mFoo[2];
};
int main()
{
FactoryC factory{};
}
and I don't want to call the default, move and copy constructor. Due to that I deleted the functions. Unfortunately this results in following error (compiled with C++17)
<source>: In constructor 'FactoryC::FactoryC()':
<source>:42:4: error: use of deleted function 'FooC::FooC(FooC&&)'
}
^
<source>:26:4: note: declared here
FooC(FooC&&) = delete;
^~~~
Compiler returned: 1
Is it possible to force in this example the calling of constructor with the parameters and still delete the default, move and copy constructor of FooC?
c++ c++11 gcc c++17
1
cannot reproduce: godbolt.org/z/ax8I1F
– bolov
Nov 19 at 20:58
Fwiw, msvc 19 (vs2015) chews this up without issue.
– WhozCraig
Nov 19 at 20:59
2
@bolov error happens with gcc 8.2. Might be a bug.
– eukaryota
Nov 19 at 20:59
5
This certainly looks like a bug. MakingInterface
destructor non-virtual (with corresponding removal of override) fixes compilation issue.
– SergeyA
Nov 19 at 21:05
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I have following example (extension to Avoid calling move constructor)
#include <cstdint>
class Interface
{
public:
Interface() = default;
virtual ~Interface() = default;
Interface(const Interface&) = delete;
Interface(Interface&&) = delete;
const Interface& operator=(const Interface&) = delete;
Interface& operator=(Interface&&) = delete;
};
class FooC : public Interface
{
public:
FooC(uint16_t iPort, uint16_t iPin)
: PORT(iPort)
, PIN(iPin)
{
};
FooC() = delete;
~FooC() override = default;
FooC(const FooC&) = delete;
FooC(FooC&&) = delete;
const FooC& operator=(const FooC&) = delete;
FooC& operator=(FooC&&) = delete;
private:
const uint16_t PORT;
const uint16_t PIN;
};
class FactoryC
{
public:
FactoryC()
: mFoo{
{1, 2},
{3, 4}
}
{
};
~FactoryC() = default;
FactoryC(const FactoryC&) = delete;
FactoryC(FactoryC&&) = delete;
const FactoryC& operator=(const FactoryC&) = delete;
FactoryC& operator=(FactoryC&&) = delete;
private:
FooC mFoo[2];
};
int main()
{
FactoryC factory{};
}
and I don't want to call the default, move and copy constructor. Due to that I deleted the functions. Unfortunately this results in following error (compiled with C++17)
<source>: In constructor 'FactoryC::FactoryC()':
<source>:42:4: error: use of deleted function 'FooC::FooC(FooC&&)'
}
^
<source>:26:4: note: declared here
FooC(FooC&&) = delete;
^~~~
Compiler returned: 1
Is it possible to force in this example the calling of constructor with the parameters and still delete the default, move and copy constructor of FooC?
c++ c++11 gcc c++17
I have following example (extension to Avoid calling move constructor)
#include <cstdint>
class Interface
{
public:
Interface() = default;
virtual ~Interface() = default;
Interface(const Interface&) = delete;
Interface(Interface&&) = delete;
const Interface& operator=(const Interface&) = delete;
Interface& operator=(Interface&&) = delete;
};
class FooC : public Interface
{
public:
FooC(uint16_t iPort, uint16_t iPin)
: PORT(iPort)
, PIN(iPin)
{
};
FooC() = delete;
~FooC() override = default;
FooC(const FooC&) = delete;
FooC(FooC&&) = delete;
const FooC& operator=(const FooC&) = delete;
FooC& operator=(FooC&&) = delete;
private:
const uint16_t PORT;
const uint16_t PIN;
};
class FactoryC
{
public:
FactoryC()
: mFoo{
{1, 2},
{3, 4}
}
{
};
~FactoryC() = default;
FactoryC(const FactoryC&) = delete;
FactoryC(FactoryC&&) = delete;
const FactoryC& operator=(const FactoryC&) = delete;
FactoryC& operator=(FactoryC&&) = delete;
private:
FooC mFoo[2];
};
int main()
{
FactoryC factory{};
}
and I don't want to call the default, move and copy constructor. Due to that I deleted the functions. Unfortunately this results in following error (compiled with C++17)
<source>: In constructor 'FactoryC::FactoryC()':
<source>:42:4: error: use of deleted function 'FooC::FooC(FooC&&)'
}
^
<source>:26:4: note: declared here
FooC(FooC&&) = delete;
^~~~
Compiler returned: 1
Is it possible to force in this example the calling of constructor with the parameters and still delete the default, move and copy constructor of FooC?
c++ c++11 gcc c++17
c++ c++11 gcc c++17
edited Nov 19 at 21:07
SergeyA
40.7k53781
40.7k53781
asked Nov 19 at 20:55
Zlatan
1409
1409
1
cannot reproduce: godbolt.org/z/ax8I1F
– bolov
Nov 19 at 20:58
Fwiw, msvc 19 (vs2015) chews this up without issue.
– WhozCraig
Nov 19 at 20:59
2
@bolov error happens with gcc 8.2. Might be a bug.
– eukaryota
Nov 19 at 20:59
5
This certainly looks like a bug. MakingInterface
destructor non-virtual (with corresponding removal of override) fixes compilation issue.
– SergeyA
Nov 19 at 21:05
add a comment |
1
cannot reproduce: godbolt.org/z/ax8I1F
– bolov
Nov 19 at 20:58
Fwiw, msvc 19 (vs2015) chews this up without issue.
– WhozCraig
Nov 19 at 20:59
2
@bolov error happens with gcc 8.2. Might be a bug.
– eukaryota
Nov 19 at 20:59
5
This certainly looks like a bug. MakingInterface
destructor non-virtual (with corresponding removal of override) fixes compilation issue.
– SergeyA
Nov 19 at 21:05
1
1
cannot reproduce: godbolt.org/z/ax8I1F
– bolov
Nov 19 at 20:58
cannot reproduce: godbolt.org/z/ax8I1F
– bolov
Nov 19 at 20:58
Fwiw, msvc 19 (vs2015) chews this up without issue.
– WhozCraig
Nov 19 at 20:59
Fwiw, msvc 19 (vs2015) chews this up without issue.
– WhozCraig
Nov 19 at 20:59
2
2
@bolov error happens with gcc 8.2. Might be a bug.
– eukaryota
Nov 19 at 20:59
@bolov error happens with gcc 8.2. Might be a bug.
– eukaryota
Nov 19 at 20:59
5
5
This certainly looks like a bug. Making
Interface
destructor non-virtual (with corresponding removal of override) fixes compilation issue.– SergeyA
Nov 19 at 21:05
This certainly looks like a bug. Making
Interface
destructor non-virtual (with corresponding removal of override) fixes compilation issue.– SergeyA
Nov 19 at 21:05
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
This appears to be a bug. @SergeyA's comment:
This certainly looks like a bug. Making Interface destructor non-virtual (with corresponding removal of override) fixes compilation issue.
suggests that the issue has to do with virtual base classes. Indeed, bug report #86849 deals with an unrelated issue and Richard Smith comes to this conclusion:
Interestingly, GCC does appear to suppress guaranteed copy elision if the class has virtual base classes.
1
There's no virtual base class in OP's code. There is a non-virtual base class with a virtual destructor
– M.M
Nov 20 at 2:03
Thanks for the hint with the bug a providing a interim solution :-)
– Zlatan
Nov 20 at 14:35
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
This appears to be a bug. @SergeyA's comment:
This certainly looks like a bug. Making Interface destructor non-virtual (with corresponding removal of override) fixes compilation issue.
suggests that the issue has to do with virtual base classes. Indeed, bug report #86849 deals with an unrelated issue and Richard Smith comes to this conclusion:
Interestingly, GCC does appear to suppress guaranteed copy elision if the class has virtual base classes.
1
There's no virtual base class in OP's code. There is a non-virtual base class with a virtual destructor
– M.M
Nov 20 at 2:03
Thanks for the hint with the bug a providing a interim solution :-)
– Zlatan
Nov 20 at 14:35
add a comment |
up vote
4
down vote
This appears to be a bug. @SergeyA's comment:
This certainly looks like a bug. Making Interface destructor non-virtual (with corresponding removal of override) fixes compilation issue.
suggests that the issue has to do with virtual base classes. Indeed, bug report #86849 deals with an unrelated issue and Richard Smith comes to this conclusion:
Interestingly, GCC does appear to suppress guaranteed copy elision if the class has virtual base classes.
1
There's no virtual base class in OP's code. There is a non-virtual base class with a virtual destructor
– M.M
Nov 20 at 2:03
Thanks for the hint with the bug a providing a interim solution :-)
– Zlatan
Nov 20 at 14:35
add a comment |
up vote
4
down vote
up vote
4
down vote
This appears to be a bug. @SergeyA's comment:
This certainly looks like a bug. Making Interface destructor non-virtual (with corresponding removal of override) fixes compilation issue.
suggests that the issue has to do with virtual base classes. Indeed, bug report #86849 deals with an unrelated issue and Richard Smith comes to this conclusion:
Interestingly, GCC does appear to suppress guaranteed copy elision if the class has virtual base classes.
This appears to be a bug. @SergeyA's comment:
This certainly looks like a bug. Making Interface destructor non-virtual (with corresponding removal of override) fixes compilation issue.
suggests that the issue has to do with virtual base classes. Indeed, bug report #86849 deals with an unrelated issue and Richard Smith comes to this conclusion:
Interestingly, GCC does appear to suppress guaranteed copy elision if the class has virtual base classes.
answered Nov 19 at 23:35
user10677333
411
411
1
There's no virtual base class in OP's code. There is a non-virtual base class with a virtual destructor
– M.M
Nov 20 at 2:03
Thanks for the hint with the bug a providing a interim solution :-)
– Zlatan
Nov 20 at 14:35
add a comment |
1
There's no virtual base class in OP's code. There is a non-virtual base class with a virtual destructor
– M.M
Nov 20 at 2:03
Thanks for the hint with the bug a providing a interim solution :-)
– Zlatan
Nov 20 at 14:35
1
1
There's no virtual base class in OP's code. There is a non-virtual base class with a virtual destructor
– M.M
Nov 20 at 2:03
There's no virtual base class in OP's code. There is a non-virtual base class with a virtual destructor
– M.M
Nov 20 at 2:03
Thanks for the hint with the bug a providing a interim solution :-)
– Zlatan
Nov 20 at 14:35
Thanks for the hint with the bug a providing a interim solution :-)
– Zlatan
Nov 20 at 14:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382501%2favoid-calling-default-move-and-copy-constructor%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
cannot reproduce: godbolt.org/z/ax8I1F
– bolov
Nov 19 at 20:58
Fwiw, msvc 19 (vs2015) chews this up without issue.
– WhozCraig
Nov 19 at 20:59
2
@bolov error happens with gcc 8.2. Might be a bug.
– eukaryota
Nov 19 at 20:59
5
This certainly looks like a bug. Making
Interface
destructor non-virtual (with corresponding removal of override) fixes compilation issue.– SergeyA
Nov 19 at 21:05