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

Beginner at C++. Get errors in the code
Code Blocks incorrect c++ output
__uint128_t not working with Clang and libstdc++
When does std::is_convertible consider primitive types convertible?
How is calling a function on a subtype implemented?
Model fitting in ceres_solver
When does PC allocate static char* inside of if block [closed]
Read from a file search for a word and copy the entire line to another file [closed]
What is the difference between these different ways to invoke an operator on `this`?
Saving variables after a form is closed in WinForms with C++
How to call OnEraseBkgnd() to repaint the window in another function? (MFC)
How can I convert this C# foreach code to C++?
Dynamic int pointer address? [duplicate]
x86 MASM - passing and accessing a 2D array
Boost.spirit x3 compiler error when using semantic action in VS15
g++ undefined reference with a shared library [duplicate]

Categories

HOME
menu
arduino
encryption
soap
sql-server-2005
cakephp-3.0
64bit
autofixture
css-modules
jasperserver
sql-azure
video.js
conv-neural-network
trackjs
google-fusion-tables
riscv
web-component
dht
derivative
azure-storage-blobs
ajaxform
cdb
summary
postmates
fork
angle
google-surveys
pgadmin
povray
tensorflow-serving
citations
angular-daterangepicker
google-optimize
codeigniter-2
leanft
angularjs-ng-model
c#-6.0
ektron
multibox
yield
ole
google-truth
smo
masm
matlab-uitable
db-schema
lab-color-space
shodan
vorlon
mongodb-scala
sqlcommand
greenlets
google-chartwrapper
canonical-link
windows-firewall-api
sequencing
smf
correspondence-analysis
ratchet
idfa
struts2-jquery-grid
rgraph
max-heap
adobe-flash-cs3
wx
offsetheight
adobe-dps
euclidean-distance
beta-testing
gstat
pull-request
jison
elasticsearch-marvel
puphpet
maven-site-plugin
jquery-dropkick
mysql-error-1093
workbench
django-1.7
tankauth
teamsite
nools
restriction
invalidation
impactjs
raii
google-playground
mapstraction
facebook-app-center
asp.net-compiler
ereg
jwebunit
timezoneoffset
objectiveflickr
silverlight-embedded
django-generic-views
xcode4.1
equivalent
xmlnodelist
widechar
table-driven

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