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

Why am I getting Error C2061? [duplicate]
In Android Studio - NDK, I need to call c++ functions that are not supported by NDK, how to do that?
Calling base class template constructor in C++
Pure virtual function
Invert orientation of memory space?
c++ parallel std::sort for floating values
how to specify search directory for source code build
How to kill boost thread from another process using thread.get_id()
can you create a c++ file from an .o object file with makefile?
Programming Homework: Robot Genetic Algorithm [closed]
Error in gauss-newton implementation for pose optimization
What does “this” refer to if object is on the stack?
performing git describe in libgit2
Gauss Jacobi method in C
How to display moving objects from a vector?
Win32 Sharing Environment Variables Across 2+ Instances of Create Process [duplicate]

Categories

HOME
git
web
data-warehouse
knn
mongoose
mono
custom-attributes
keyboard-shortcuts
etcd
google-places-api
chromium
mips32
put
sage
http-headers
google-vr
url-rewriting
google-cardboard
android-toast
delaunay
lokijs
axon
cassandra-cli
sensenet
jxmaps
spring-data-elasticsearch
atlassian-sourcetree
biopython
konvajs
nlog
twitter-streaming-api
gulp-sass
firebase-cloud-messaging
intercept
xml-configuration
wijmo5
mahapps.metro
jquery-chosen
choropleth
infowindow
gsutil
cassandra-3.0
leanft
war
data-storage
card-flip
redhat-bpm
dataweave
homescreen
moa
digestive-functors
fixture
gulp-usemin
nsset
yield
autocorrect
http-status-code-415
ifttt
smo
jquery-ui-widget-factory
preferencefragment
monocle-scala
c++1z
ucos
on-screen-keyboard
magicalrecord
ember-cli-mirage
google-gadget
local-variables
spock-reports
devpartner
iis-8.5
amazon-mobile-analytics
dfdl
integer-overflow
swift-mt
assignment-operator
argo
7digital
mpmediapickercontroller
html5-template
cordova-chrome-app
wunderlist
ontouchevent
t4-toolbox
magnetometer
sapb1
jquery-dynatree
review
image-manipulation
httplib
fad
lcm
facade
jekyll-extensions
duplicate-removal
openacs
restkit-0.20
kiwi
factoring
microsoft-commerce-server
parallel.for
django-1.2
rs485
mib
jsonexception
file-association
lytebox
enctype
gwt-2.5
alpha-five
custom-view
viewstack
actinic
mindmapping
iphone-sdk-4.1
blat
open-basedir
microsoft.build

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile