c++


Why can I prevent implicit conversions for primitives but not user-defined types?


The High Integrity C++ Standards suggest that rvalue arguments to functions can be deleted thus preventing implicit conversions.
http://www.codingstandard.com/rule/8-3-4-define-delete-functions-with-parameters-of-type-rvalue-reference-to-const/
I've found that the behaviour for primitives and user-defined types is very different.
struct A { };
struct B { B(const A& ) {} };
template <class T>
void foo(const T&&) = delete; // 1 - deleted rvalue overload. const intentional.
void foo(B) {} // 2
void foo(int) {} // 3
int main(int argc, char* argv[])
{
A a;
foo(a); // This resolves to 2
foo(3.3); // This resolves to 1
foo(2); // This resolves to 3 (as expected).
}
Why does a deleted rvalue overload prevent an implicit conversion to int but not from one user-defined type to another?
In your code, there is no difference in treatment between user-defined types and primitive types. The difference between the behaviour of these two lines:
foo(a);
foo(3.3);
is that a is an lvalue, and 3.3 is an rvalue. The rvalue argument matches your overload 1 (which only accepts rvalues), the lvalue argument does not.
If you try to invoke foo<A> with an rvalue argument it will also match 1 and fail, e.g. foo(A{});.
The High Integrity C++ Standards suggest that rvalue arguments to
functions can be deleted thus preventing implicit conversions.
No, only a forwarding reference overload disables ICS (Implicit Conversion Sequence) for all other overloads in the overload set. Make it a forwarding reference, and see ICS disabled (Coliru Link)
template <class T>
void foo(const T&&) = delete; // introduces a qualification match
The above code adds a qualification match to the overload. Thus, ICS is still in play.
Why foo(3.3) failed is because 3.3 is an prvalue of type double which will match better with the rvalue overload than converting to int. Because qualification match is ranked better than a conversion match
There are 3 possible overloads
1 is viable.
2 is viable
3 isn't
2 is better match (template (non exact match) vs regular method (with one user define conversion)).
You may look at http://en.cppreference.com/w/cpp/language/overload_resolution
to see a complete set of rules needed

Related Links

Word frequency program - file input too large?
integer minus a class [on hold]
Amazon AWS CPP SDK Get S3 Bucket Download Progress
IDEA CLion 2016.2.1 can't recognize the function exit()
Audacity PCM Data [on hold]
Best practice to set up qt application that works for different DPI
openssl fips error on arm Linux
C++ - Pretty way to make frequencies add up to 1
integerset obtaining the given array
Arduino Invalid conversion from 'char' to 'char*'
virtual ClistCtrl with checkboxes on displayed report list style
Error when modifying vector member of another class
Interrupt Service Routine can't be called directly
Lambda capture by default reference vs named
convert bitset<64> to string every 8 bit
argument of type “int” incompatible with parameter of type “int”

Categories

HOME
parsing
logic
raster
reporting-services
solr6
selenium-chromedriver
autodesk-forge
bitcoin
bootstrap-datepicker
mapkit
bpm
log4net
pthreads
wmi-query
kafka-consumer-api
twincat-ads
markup
netbeans-7.3
serenity-bdd
mdm
xmlstarlet
postmates
phylogeny
btrfs
android-appbarlayout
bing-api
android-logcat
byte
induction
yamldotnet
state
easymock
azure-virtual-machine
codeigniter-2
war
dataweave
meta-tags
marytts
mlt
reddit
auto-value
lighttable
ssas-tabular
nsset
ranorex
tivoli-identity-manager
proof-of-correctness
erpnext
shared-ptr
jsdoc3
unicode-string
istanbul
ember-cli-mirage
synopsys-vcs
gm
owner
google-plus-domains
testdroid
little-endian
zend-studio
couchbase-sync-gateway
elmah.mvc
tstringgrid
fileapi
xlet
tap
reagent
wave
treenode
topological-sort
hashbytes
jxa
like
sql-job
wtl
swift-protocols
ransac
orca
mux
subsonic2.2
xcode-instruments
worklight-server
pixate
cxf-client
android-x86
worklight-studio
resourcedictionary
phpmotion
dbase
mat-file
firepad
eclipse-wtp
camping
struts-config
nested-queries
mobile-phones
testdriven.net
iphone-3gs
httpverbs
asp.net-mvc-2-validation
html-rendering
pvcs

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile