Avoid calling move constructor











up vote
13
down vote

favorite
2












I have following example



#include <cstdint>

class FooC
{
public:
FooC(uint16_t iPort, uint16_t iPin)
: PORT(iPort)
, PIN(iPin)
{
};

~FooC() = default;

FooC() = delete;
FooC(const FooC&) = delete;
FooC(FooC&&) = delete;

private:
const uint16_t PORT;
const uint16_t PIN;
};

int main()
{
FooC array[2] = {
FooC(1,2),
FooC(3,4)
};
}


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++11)




: In function 'int main()':



:28:5: error: use of deleted function 'FooC::FooC(FooC&&)'



 };

^


:16:4: note: declared here



FooC(FooC&&) = delete;

^~~~


:28:5: error: use of deleted function 'FooC::FooC(FooC&&)'



 };

^


:16: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?










share|improve this question


















  • 5




    use C++17 :-)...
    – marcinj
    Nov 19 at 19:14






  • 1




    Since you've deleted default copy and move constructors (as well as assignment oprators) your class is no longer movable and copyable, when your program expecting move assignable class.
    – Victor Gubin
    Nov 19 at 19:15








  • 2




    Have you tried removing the =
    – JVApen
    Nov 19 at 19:17






  • 1




    And constexpr FooC(uint16_t iPort, uint16_t iPin) noexcept
    – Victor Gubin
    Nov 19 at 19:22










  • Note that you don't need to delete the move-constructor: when you delete the copy-constructor, the move-constructor is not implicitly generated
    – M.M
    Nov 19 at 23:03















up vote
13
down vote

favorite
2












I have following example



#include <cstdint>

class FooC
{
public:
FooC(uint16_t iPort, uint16_t iPin)
: PORT(iPort)
, PIN(iPin)
{
};

~FooC() = default;

FooC() = delete;
FooC(const FooC&) = delete;
FooC(FooC&&) = delete;

private:
const uint16_t PORT;
const uint16_t PIN;
};

int main()
{
FooC array[2] = {
FooC(1,2),
FooC(3,4)
};
}


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++11)




: In function 'int main()':



:28:5: error: use of deleted function 'FooC::FooC(FooC&&)'



 };

^


:16:4: note: declared here



FooC(FooC&&) = delete;

^~~~


:28:5: error: use of deleted function 'FooC::FooC(FooC&&)'



 };

^


:16: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?










share|improve this question


















  • 5




    use C++17 :-)...
    – marcinj
    Nov 19 at 19:14






  • 1




    Since you've deleted default copy and move constructors (as well as assignment oprators) your class is no longer movable and copyable, when your program expecting move assignable class.
    – Victor Gubin
    Nov 19 at 19:15








  • 2




    Have you tried removing the =
    – JVApen
    Nov 19 at 19:17






  • 1




    And constexpr FooC(uint16_t iPort, uint16_t iPin) noexcept
    – Victor Gubin
    Nov 19 at 19:22










  • Note that you don't need to delete the move-constructor: when you delete the copy-constructor, the move-constructor is not implicitly generated
    – M.M
    Nov 19 at 23:03













up vote
13
down vote

favorite
2









up vote
13
down vote

favorite
2






2





I have following example



#include <cstdint>

class FooC
{
public:
FooC(uint16_t iPort, uint16_t iPin)
: PORT(iPort)
, PIN(iPin)
{
};

~FooC() = default;

FooC() = delete;
FooC(const FooC&) = delete;
FooC(FooC&&) = delete;

private:
const uint16_t PORT;
const uint16_t PIN;
};

int main()
{
FooC array[2] = {
FooC(1,2),
FooC(3,4)
};
}


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++11)




: In function 'int main()':



:28:5: error: use of deleted function 'FooC::FooC(FooC&&)'



 };

^


:16:4: note: declared here



FooC(FooC&&) = delete;

^~~~


:28:5: error: use of deleted function 'FooC::FooC(FooC&&)'



 };

^


:16: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?










share|improve this question













I have following example



#include <cstdint>

class FooC
{
public:
FooC(uint16_t iPort, uint16_t iPin)
: PORT(iPort)
, PIN(iPin)
{
};

~FooC() = default;

FooC() = delete;
FooC(const FooC&) = delete;
FooC(FooC&&) = delete;

private:
const uint16_t PORT;
const uint16_t PIN;
};

int main()
{
FooC array[2] = {
FooC(1,2),
FooC(3,4)
};
}


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++11)




: In function 'int main()':



:28:5: error: use of deleted function 'FooC::FooC(FooC&&)'



 };

^


:16:4: note: declared here



FooC(FooC&&) = delete;

^~~~


:28:5: error: use of deleted function 'FooC::FooC(FooC&&)'



 };

^


:16: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?







c++ c++11 c++17






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 19:11









Zlatan

14810




14810








  • 5




    use C++17 :-)...
    – marcinj
    Nov 19 at 19:14






  • 1




    Since you've deleted default copy and move constructors (as well as assignment oprators) your class is no longer movable and copyable, when your program expecting move assignable class.
    – Victor Gubin
    Nov 19 at 19:15








  • 2




    Have you tried removing the =
    – JVApen
    Nov 19 at 19:17






  • 1




    And constexpr FooC(uint16_t iPort, uint16_t iPin) noexcept
    – Victor Gubin
    Nov 19 at 19:22










  • Note that you don't need to delete the move-constructor: when you delete the copy-constructor, the move-constructor is not implicitly generated
    – M.M
    Nov 19 at 23:03














  • 5




    use C++17 :-)...
    – marcinj
    Nov 19 at 19:14






  • 1




    Since you've deleted default copy and move constructors (as well as assignment oprators) your class is no longer movable and copyable, when your program expecting move assignable class.
    – Victor Gubin
    Nov 19 at 19:15








  • 2




    Have you tried removing the =
    – JVApen
    Nov 19 at 19:17






  • 1




    And constexpr FooC(uint16_t iPort, uint16_t iPin) noexcept
    – Victor Gubin
    Nov 19 at 19:22










  • Note that you don't need to delete the move-constructor: when you delete the copy-constructor, the move-constructor is not implicitly generated
    – M.M
    Nov 19 at 23:03








5




5




use C++17 :-)...
– marcinj
Nov 19 at 19:14




use C++17 :-)...
– marcinj
Nov 19 at 19:14




1




1




Since you've deleted default copy and move constructors (as well as assignment oprators) your class is no longer movable and copyable, when your program expecting move assignable class.
– Victor Gubin
Nov 19 at 19:15






Since you've deleted default copy and move constructors (as well as assignment oprators) your class is no longer movable and copyable, when your program expecting move assignable class.
– Victor Gubin
Nov 19 at 19:15






2




2




Have you tried removing the =
– JVApen
Nov 19 at 19:17




Have you tried removing the =
– JVApen
Nov 19 at 19:17




1




1




And constexpr FooC(uint16_t iPort, uint16_t iPin) noexcept
– Victor Gubin
Nov 19 at 19:22




And constexpr FooC(uint16_t iPort, uint16_t iPin) noexcept
– Victor Gubin
Nov 19 at 19:22












Note that you don't need to delete the move-constructor: when you delete the copy-constructor, the move-constructor is not implicitly generated
– M.M
Nov 19 at 23:03




Note that you don't need to delete the move-constructor: when you delete the copy-constructor, the move-constructor is not implicitly generated
– M.M
Nov 19 at 23:03












2 Answers
2






active

oldest

votes

















up vote
14
down vote













In C++11 and C++14, you can use nested braces:



FooC array[2] = {{1,2}, {3,4}};


In C++17 your code should already work as written thanks to the new prvalue/materialization rules ("guaranteed copy elision").






share|improve this answer

















  • 1




    C++17 demo
    – Kerrek SB
    Nov 19 at 19:15


















up vote
5
down vote














Is it possible to force in this example the calling of constructor with the parameters and still delete the default, move and copy constructor?




No with your current syntax (before C++17) and yes (in C++17).



Pre-C++17:



This is not possible. The aggregate initialization copies the initializers into the aggregate. This means you have to have an accessible copy/move constructor. In C++11 you have to pass the constructor parameters as their own braced-init-list. This means you aren't copying FooC's but instead copy-list-initializing the FooC's in the array which calls the 2 parameter constructor instead of the copy/move constructor.



FooC array[2] = {
{1, 2},
{3, 4}
};


C++17:



You no longer have temporary objects in the braced-init-list and each element of the array will be directly initialized instead of copy initialized.






share|improve this answer























  • I'd be careful with the phrasing here. Your pre-C++17 example is copy-list-initializing the elements.
    – T.C.
    Nov 19 at 22:11










  • @T.C. Wording updated.
    – NathanOliver
    Nov 19 at 22:15











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%2f53381152%2favoid-calling-move-constructor%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
14
down vote













In C++11 and C++14, you can use nested braces:



FooC array[2] = {{1,2}, {3,4}};


In C++17 your code should already work as written thanks to the new prvalue/materialization rules ("guaranteed copy elision").






share|improve this answer

















  • 1




    C++17 demo
    – Kerrek SB
    Nov 19 at 19:15















up vote
14
down vote













In C++11 and C++14, you can use nested braces:



FooC array[2] = {{1,2}, {3,4}};


In C++17 your code should already work as written thanks to the new prvalue/materialization rules ("guaranteed copy elision").






share|improve this answer

















  • 1




    C++17 demo
    – Kerrek SB
    Nov 19 at 19:15













up vote
14
down vote










up vote
14
down vote









In C++11 and C++14, you can use nested braces:



FooC array[2] = {{1,2}, {3,4}};


In C++17 your code should already work as written thanks to the new prvalue/materialization rules ("guaranteed copy elision").






share|improve this answer












In C++11 and C++14, you can use nested braces:



FooC array[2] = {{1,2}, {3,4}};


In C++17 your code should already work as written thanks to the new prvalue/materialization rules ("guaranteed copy elision").







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 19:15









Kerrek SB

361k61677914




361k61677914








  • 1




    C++17 demo
    – Kerrek SB
    Nov 19 at 19:15














  • 1




    C++17 demo
    – Kerrek SB
    Nov 19 at 19:15








1




1




C++17 demo
– Kerrek SB
Nov 19 at 19:15




C++17 demo
– Kerrek SB
Nov 19 at 19:15












up vote
5
down vote














Is it possible to force in this example the calling of constructor with the parameters and still delete the default, move and copy constructor?




No with your current syntax (before C++17) and yes (in C++17).



Pre-C++17:



This is not possible. The aggregate initialization copies the initializers into the aggregate. This means you have to have an accessible copy/move constructor. In C++11 you have to pass the constructor parameters as their own braced-init-list. This means you aren't copying FooC's but instead copy-list-initializing the FooC's in the array which calls the 2 parameter constructor instead of the copy/move constructor.



FooC array[2] = {
{1, 2},
{3, 4}
};


C++17:



You no longer have temporary objects in the braced-init-list and each element of the array will be directly initialized instead of copy initialized.






share|improve this answer























  • I'd be careful with the phrasing here. Your pre-C++17 example is copy-list-initializing the elements.
    – T.C.
    Nov 19 at 22:11










  • @T.C. Wording updated.
    – NathanOliver
    Nov 19 at 22:15















up vote
5
down vote














Is it possible to force in this example the calling of constructor with the parameters and still delete the default, move and copy constructor?




No with your current syntax (before C++17) and yes (in C++17).



Pre-C++17:



This is not possible. The aggregate initialization copies the initializers into the aggregate. This means you have to have an accessible copy/move constructor. In C++11 you have to pass the constructor parameters as their own braced-init-list. This means you aren't copying FooC's but instead copy-list-initializing the FooC's in the array which calls the 2 parameter constructor instead of the copy/move constructor.



FooC array[2] = {
{1, 2},
{3, 4}
};


C++17:



You no longer have temporary objects in the braced-init-list and each element of the array will be directly initialized instead of copy initialized.






share|improve this answer























  • I'd be careful with the phrasing here. Your pre-C++17 example is copy-list-initializing the elements.
    – T.C.
    Nov 19 at 22:11










  • @T.C. Wording updated.
    – NathanOliver
    Nov 19 at 22:15













up vote
5
down vote










up vote
5
down vote










Is it possible to force in this example the calling of constructor with the parameters and still delete the default, move and copy constructor?




No with your current syntax (before C++17) and yes (in C++17).



Pre-C++17:



This is not possible. The aggregate initialization copies the initializers into the aggregate. This means you have to have an accessible copy/move constructor. In C++11 you have to pass the constructor parameters as their own braced-init-list. This means you aren't copying FooC's but instead copy-list-initializing the FooC's in the array which calls the 2 parameter constructor instead of the copy/move constructor.



FooC array[2] = {
{1, 2},
{3, 4}
};


C++17:



You no longer have temporary objects in the braced-init-list and each element of the array will be directly initialized instead of copy initialized.






share|improve this answer















Is it possible to force in this example the calling of constructor with the parameters and still delete the default, move and copy constructor?




No with your current syntax (before C++17) and yes (in C++17).



Pre-C++17:



This is not possible. The aggregate initialization copies the initializers into the aggregate. This means you have to have an accessible copy/move constructor. In C++11 you have to pass the constructor parameters as their own braced-init-list. This means you aren't copying FooC's but instead copy-list-initializing the FooC's in the array which calls the 2 parameter constructor instead of the copy/move constructor.



FooC array[2] = {
{1, 2},
{3, 4}
};


C++17:



You no longer have temporary objects in the braced-init-list and each element of the array will be directly initialized instead of copy initialized.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 at 22:14

























answered Nov 19 at 19:17









NathanOliver

85.5k15118177




85.5k15118177












  • I'd be careful with the phrasing here. Your pre-C++17 example is copy-list-initializing the elements.
    – T.C.
    Nov 19 at 22:11










  • @T.C. Wording updated.
    – NathanOliver
    Nov 19 at 22:15


















  • I'd be careful with the phrasing here. Your pre-C++17 example is copy-list-initializing the elements.
    – T.C.
    Nov 19 at 22:11










  • @T.C. Wording updated.
    – NathanOliver
    Nov 19 at 22:15
















I'd be careful with the phrasing here. Your pre-C++17 example is copy-list-initializing the elements.
– T.C.
Nov 19 at 22:11




I'd be careful with the phrasing here. Your pre-C++17 example is copy-list-initializing the elements.
– T.C.
Nov 19 at 22:11












@T.C. Wording updated.
– NathanOliver
Nov 19 at 22:15




@T.C. Wording updated.
– NathanOliver
Nov 19 at 22:15


















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%2f53381152%2favoid-calling-move-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

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents