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?










share|improve this question




















  • 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















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?










share|improve this question




















  • 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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. Making Interface destructor non-virtual (with corresponding removal of override) fixes compilation issue.
    – SergeyA
    Nov 19 at 21:05














  • 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








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












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.







share|improve this answer

















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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.







share|improve this answer

















  • 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















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.







share|improve this answer

















  • 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













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.







share|improve this answer












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.








share|improve this answer












share|improve this answer



share|improve this answer










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














  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen