Avoiding Implicit Conversion in Constructor. Explicit keyword doesn't help hereWhat does the explicit keyword mean?Can you use keyword explicit to prevent automatic conversion of method parameters?Can a single argument constructor with a default value be subject to implicit type conversionwhy constructors aren't explicit by default?What could go wrong if copy-list-initialization allowed explicit constructors?Explicit conversion functions, direct-initialization, and converting constructorsHow to provide implicit and explicit conversion ctr for same type?Implicit conversion from user-defined type to primitive type in C++Is list-initialization an implicit conversion?How to combine type constraints & implicit conversions with C++11 universal references?
Why are we moving in circles with a tandem kayak?
How can Paypal know my card is being used in another account?
Using Python in a Bash Script
How to efficiently shred a lot of cabbage?
May a hotel provide accommodation for fewer people than booked?
How to prevent a single-element caster from being useless against immune foes?
How did astronauts using rovers tell direction without compasses on the Moon?
When to sell a coin collection
When does the Homunculus die, exactly?
Bouncing map back into its bounds, after user dragged it out
What force enables us to walk? Friction or normal reaction?
How to innovate in OR
"DDoouubbllee ssppeeaakk!!"
left ... right make different sizes in numerator and denominator
How to have poached eggs in "sphere form"?
Rampant sharing of authorship among colleagues in the name of "collaboration". Is not taking part in it a death knell for a future in academia?
Should I put my name first, or last in the team members list
Do the books ever say oliphaunts aren’t elephants?
When encrypting twice with two separate keys, can a single key decrypt both steps?
What is the highest achievable score in Catan
Are all French verb conjugation tenses and moods practical and efficient?
Just how much information should you share with a former client?
Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?
Why did some Apollo missions carry a grenade launcher?
Avoiding Implicit Conversion in Constructor. Explicit keyword doesn't help here
What does the explicit keyword mean?Can you use keyword explicit to prevent automatic conversion of method parameters?Can a single argument constructor with a default value be subject to implicit type conversionwhy constructors aren't explicit by default?What could go wrong if copy-list-initialization allowed explicit constructors?Explicit conversion functions, direct-initialization, and converting constructorsHow to provide implicit and explicit conversion ctr for same type?Implicit conversion from user-defined type to primitive type in C++Is list-initialization an implicit conversion?How to combine type constraints & implicit conversions with C++11 universal references?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am able to avoid the implicit conversion of constructor using explicit keyword. So now, conversions like A a1 = 10; can be avoided.
But still I can initialize A a1 = A(20.2);. How can I disable the object creation such that only object can be created if we pass integer as a parameter e.g. A a1 = A(10)?
#include <iostream>
class A
public :
explicit A(int a)
num = a;
int num;
;
int main()
A a1=A(10.0);
std::cout << a1.num;
return 0;
c++ c++11
add a comment |
I am able to avoid the implicit conversion of constructor using explicit keyword. So now, conversions like A a1 = 10; can be avoided.
But still I can initialize A a1 = A(20.2);. How can I disable the object creation such that only object can be created if we pass integer as a parameter e.g. A a1 = A(10)?
#include <iostream>
class A
public :
explicit A(int a)
num = a;
int num;
;
int main()
A a1=A(10.0);
std::cout << a1.num;
return 0;
c++ c++11
add a comment |
I am able to avoid the implicit conversion of constructor using explicit keyword. So now, conversions like A a1 = 10; can be avoided.
But still I can initialize A a1 = A(20.2);. How can I disable the object creation such that only object can be created if we pass integer as a parameter e.g. A a1 = A(10)?
#include <iostream>
class A
public :
explicit A(int a)
num = a;
int num;
;
int main()
A a1=A(10.0);
std::cout << a1.num;
return 0;
c++ c++11
I am able to avoid the implicit conversion of constructor using explicit keyword. So now, conversions like A a1 = 10; can be avoided.
But still I can initialize A a1 = A(20.2);. How can I disable the object creation such that only object can be created if we pass integer as a parameter e.g. A a1 = A(10)?
#include <iostream>
class A
public :
explicit A(int a)
num = a;
int num;
;
int main()
A a1=A(10.0);
std::cout << a1.num;
return 0;
c++ c++11
c++ c++11
edited 8 hours ago
YSC
27.1k6 gold badges59 silver badges114 bronze badges
27.1k6 gold badges59 silver badges114 bronze badges
asked 8 hours ago
Gurpreet DhamiGurpreet Dhami
513 bronze badges
513 bronze badges
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can delete A::A(<anything not an int>);:
struct A
explicit A(int a)
: num(a)
template<class T>
A(T) = delete;
int num;
;
int main()
//A a1=A(10.0); // error: use of deleted function 'A::A(T) [with T = double]'
A a2 = A(10); // OK
(void) a2;
Demo: https://coliru.stacked-crooked.com/a/425afc19003697c9
add a comment |
The way to achieve this is to provide another constructor that would be a better match, and then delete it so you'll get an error. For your class, adding
template <typename T>
A(T) = delete;
Will stop the class from being constructed from anything that isn't an int
add a comment |
Explicitly delete the constructor for double (possibly add float):
A(double) = delete;
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f57293752%2favoiding-implicit-conversion-in-constructor-explicit-keyword-doesnt-help-here%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can delete A::A(<anything not an int>);:
struct A
explicit A(int a)
: num(a)
template<class T>
A(T) = delete;
int num;
;
int main()
//A a1=A(10.0); // error: use of deleted function 'A::A(T) [with T = double]'
A a2 = A(10); // OK
(void) a2;
Demo: https://coliru.stacked-crooked.com/a/425afc19003697c9
add a comment |
You can delete A::A(<anything not an int>);:
struct A
explicit A(int a)
: num(a)
template<class T>
A(T) = delete;
int num;
;
int main()
//A a1=A(10.0); // error: use of deleted function 'A::A(T) [with T = double]'
A a2 = A(10); // OK
(void) a2;
Demo: https://coliru.stacked-crooked.com/a/425afc19003697c9
add a comment |
You can delete A::A(<anything not an int>);:
struct A
explicit A(int a)
: num(a)
template<class T>
A(T) = delete;
int num;
;
int main()
//A a1=A(10.0); // error: use of deleted function 'A::A(T) [with T = double]'
A a2 = A(10); // OK
(void) a2;
Demo: https://coliru.stacked-crooked.com/a/425afc19003697c9
You can delete A::A(<anything not an int>);:
struct A
explicit A(int a)
: num(a)
template<class T>
A(T) = delete;
int num;
;
int main()
//A a1=A(10.0); // error: use of deleted function 'A::A(T) [with T = double]'
A a2 = A(10); // OK
(void) a2;
Demo: https://coliru.stacked-crooked.com/a/425afc19003697c9
answered 8 hours ago
YSCYSC
27.1k6 gold badges59 silver badges114 bronze badges
27.1k6 gold badges59 silver badges114 bronze badges
add a comment |
add a comment |
The way to achieve this is to provide another constructor that would be a better match, and then delete it so you'll get an error. For your class, adding
template <typename T>
A(T) = delete;
Will stop the class from being constructed from anything that isn't an int
add a comment |
The way to achieve this is to provide another constructor that would be a better match, and then delete it so you'll get an error. For your class, adding
template <typename T>
A(T) = delete;
Will stop the class from being constructed from anything that isn't an int
add a comment |
The way to achieve this is to provide another constructor that would be a better match, and then delete it so you'll get an error. For your class, adding
template <typename T>
A(T) = delete;
Will stop the class from being constructed from anything that isn't an int
The way to achieve this is to provide another constructor that would be a better match, and then delete it so you'll get an error. For your class, adding
template <typename T>
A(T) = delete;
Will stop the class from being constructed from anything that isn't an int
answered 8 hours ago
NathanOliverNathanOliver
110k19 gold badges167 silver badges246 bronze badges
110k19 gold badges167 silver badges246 bronze badges
add a comment |
add a comment |
Explicitly delete the constructor for double (possibly add float):
A(double) = delete;
add a comment |
Explicitly delete the constructor for double (possibly add float):
A(double) = delete;
add a comment |
Explicitly delete the constructor for double (possibly add float):
A(double) = delete;
Explicitly delete the constructor for double (possibly add float):
A(double) = delete;
answered 8 hours ago
Sombrero ChickenSombrero Chicken
27.4k3 gold badges36 silver badges85 bronze badges
27.4k3 gold badges36 silver badges85 bronze badges
add a comment |
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.
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%2f57293752%2favoiding-implicit-conversion-in-constructor-explicit-keyword-doesnt-help-here%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