Avoid calling move constructor
up vote
11
down vote
favorite
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
add a comment |
up vote
11
down vote
favorite
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
5
use C++17 :-)...
– marcinj
2 days ago
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
2 days ago
2
Have you tried removing the=
– JVApen
2 days ago
1
Andconstexpr FooC(uint16_t iPort, uint16_t iPin) noexcept
– Victor Gubin
2 days ago
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
2 days ago
add a comment |
up vote
11
down vote
favorite
up vote
11
down vote
favorite
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
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
c++ c++11 c++17
asked 2 days ago
Zlatan
1409
1409
5
use C++17 :-)...
– marcinj
2 days ago
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
2 days ago
2
Have you tried removing the=
– JVApen
2 days ago
1
Andconstexpr FooC(uint16_t iPort, uint16_t iPin) noexcept
– Victor Gubin
2 days ago
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
2 days ago
add a comment |
5
use C++17 :-)...
– marcinj
2 days ago
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
2 days ago
2
Have you tried removing the=
– JVApen
2 days ago
1
Andconstexpr FooC(uint16_t iPort, uint16_t iPin) noexcept
– Victor Gubin
2 days ago
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
2 days ago
5
5
use C++17 :-)...
– marcinj
2 days ago
use C++17 :-)...
– marcinj
2 days ago
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
2 days ago
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
2 days ago
2
2
Have you tried removing the
=
– JVApen
2 days ago
Have you tried removing the
=
– JVApen
2 days ago
1
1
And
constexpr FooC(uint16_t iPort, uint16_t iPin) noexcept
– Victor Gubin
2 days ago
And
constexpr FooC(uint16_t iPort, uint16_t iPin) noexcept
– Victor Gubin
2 days ago
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
2 days ago
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
2 days ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
13
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").
1
C++17 demo
– Kerrek SB
2 days ago
add a comment |
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.
I'd be careful with the phrasing here. Your pre-C++17 example is copy-list-initializing the elements.
– T.C.
2 days ago
@T.C. Wording updated.
– NathanOliver
2 days ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
13
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").
1
C++17 demo
– Kerrek SB
2 days ago
add a comment |
up vote
13
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").
1
C++17 demo
– Kerrek SB
2 days ago
add a comment |
up vote
13
down vote
up vote
13
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").
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").
answered 2 days ago
Kerrek SB
359k60674910
359k60674910
1
C++17 demo
– Kerrek SB
2 days ago
add a comment |
1
C++17 demo
– Kerrek SB
2 days ago
1
1
C++17 demo
– Kerrek SB
2 days ago
C++17 demo
– Kerrek SB
2 days ago
add a comment |
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.
I'd be careful with the phrasing here. Your pre-C++17 example is copy-list-initializing the elements.
– T.C.
2 days ago
@T.C. Wording updated.
– NathanOliver
2 days ago
add a comment |
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.
I'd be careful with the phrasing here. Your pre-C++17 example is copy-list-initializing the elements.
– T.C.
2 days ago
@T.C. Wording updated.
– NathanOliver
2 days ago
add a comment |
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.
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.
edited 2 days ago
answered 2 days ago


NathanOliver
82.7k15112172
82.7k15112172
I'd be careful with the phrasing here. Your pre-C++17 example is copy-list-initializing the elements.
– T.C.
2 days ago
@T.C. Wording updated.
– NathanOliver
2 days ago
add a comment |
I'd be careful with the phrasing here. Your pre-C++17 example is copy-list-initializing the elements.
– T.C.
2 days ago
@T.C. Wording updated.
– NathanOliver
2 days ago
I'd be careful with the phrasing here. Your pre-C++17 example is copy-list-initializing the elements.
– T.C.
2 days ago
I'd be careful with the phrasing here. Your pre-C++17 example is copy-list-initializing the elements.
– T.C.
2 days ago
@T.C. Wording updated.
– NathanOliver
2 days ago
@T.C. Wording updated.
– NathanOliver
2 days ago
add a comment |
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%2f53381152%2favoid-calling-move-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
5
use C++17 :-)...
– marcinj
2 days ago
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
2 days ago
2
Have you tried removing the
=
– JVApen
2 days ago
1
And
constexpr FooC(uint16_t iPort, uint16_t iPin) noexcept
– Victor Gubin
2 days ago
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
2 days ago