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

c++: use Visual Basic dll
sprintf_s failed in release mode, and ok in debug
Measuring performance of vector<unique_ptr> on VS2013?
How to dump game data before closing C++ Windows
Accessing COM device names in C++/Win7
inverse fft producing close but wrong output
Using wrap with '-u' flag of gcc
Counting the number of lines in a C++ program leaving comments
Profiling SWIG Python code
how can I use WM_GETMINMAXINFO in a c++ project?
what is the proper way of handling the errors when working with threads?
Clang does not find function instantiated after function definition in template context
Nested lambda expressions are very slow to compile and generate huge object file by Visual C++
Access violation on program termination C++
Moving the cursor to the bottom left of the console
List of subclass “master copies”, looking for a better design

Categories

HOME
docker
qt5
cron
sitecore8
reporting-services
firebase-authentication
hardware
asp-classic
fiware
dfa
userform
q-learning
jaxb
android-gradle
ejb
vaadin-grid
servo
autofixture
crystal-reports-xi
zk
docbook
junit4
redactor
sensenet
nlog
multiline
probability-theory
mysql-error-1064
ssrs-2016
revit
bing-api
geckofx
week-number
slamdata
artoolkit
lektor
webdrivermanager-java
interactive
nerdtree
ats
3d-modelling
spring-cloud-sleuth
inline
mlt
google-console-developer
ranorex
ctags
erpnext
dispy
schemaless
rpy2
nsoutlineview
sonarqube-4.5
compass
idfa
amazon-mobile-analytics
blackberry-simulator
zodb
cirqus
distinct-on
7digital
android-bundle
pig-udf
sapb1
ews-managed-api
class-relationship
linux-capabilities
mooc
desktop-shortcut
editplus
javascript-databinding
xcode6.1
teamsite
code-metrics
kooboo
bonsaijs
ios-provisioning
citrix-access-gateway
boost-spirit-karma
circular-reference
agi
cvxopt
django-staticfiles
ldapconnection
sequence-alignment
mysql-error-1052
grit
visual-studio-test-runner
alpha-five
clang-complete
custom-view
ctime
function-overloading
kiln
iasyncresult
ant4eclipse
dynamic-css
urldecode
olpc
reverse-debugging

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App