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

Moving from old C-style pointer to C++ smart pointers with little changes in the code?
get current time in specific timezone using c++
Does compiled Crypto++ library code that uses AES/GCM encryption utilize Intel's AES-NI instructions?
Bad Memory Allocation when using Frozen Graph
QT project crashing on profile, running with release and debugger
Using reinterpret_cast to convert integer to pointer and back to integer [duplicate]
C++ suduko puzzle, initialising grid - newbie [closed]
How to swap lines in a text file?
Parent variadic function template specialized in child
How does the quality of 128 bit MurmurHash3 change in case of small key length or output truncation?
Raw socket SYN vs. packet socket SYN
How to construct a list of files from the `start group … end group` linker option
How to exclude isolated nodes from generated Doxygen Class Hierarchy
Why might compiling with -march=native cause “pointer being freed was not allocated”?
Cant access correct number of vertices for a 3d model (.OBJ) using Assimp
In using-declaration, can dependent names render to constructors after template substitution?

Categories

HOME
rstudio
rabbitmq
sql-server-2005
authorization
get
ipad
caffe
imageview
import
binary
ais
graphviz
url-rewriting
osticket
uinavigationcontroller
google-cardboard
geometry
visio
ccache
jspdf
webassembly
angularjs-ng-route
rinsim
selenium-rc
bootstrap-select
visualvm
workspace
xbuild
python-datetime
angle
avaya
rubinius
u2
pgadmin
multiple-domains
asp.net-mvc-routing
gulp-4
uppercase
appery.io
get-eventlog
nestedscrollview
regedit
paypal-adaptive-payments
burn
rufus-scheduler
tiles
mypy
webrole
jedi-vim
orders
topshelf
google-keep
playstation
cpio
syncsort-dmx-h
reportviewer2008
dart-polymer
display-bugs
eclipse-indigo
owner
lzma
ienumerable
oslo
user-mode-linux
data-connections
spawn-fcgi
stl-format
customdialog
r1soft
angular-component-router
stunnel
singularitygs
command-line-tool
fody-propertychanged
zshrc
bitarray
typescript1.7
kirby
event-loop
treenode
circular-buffer
zooming
image-manipulation
gliffy
ember.js-view
accessdatasource
datacontext
psychology
nsurlprotocol
kendo-validator
google-email-settings-api
nsfontmanager
real-time-data
wsimport
amber-smalltalk
hiding
mysql-5.0
operational-transform
dynamic-content
winqual
dft
dynamic-css
orbited

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile