I was watching a video from, //build/ and several of the MS developers were using a syntax like this in their C++11 programs:
auto foo = ref new Foo();
I understand what everything does in this line except "ref". What does that mean?
I was watching a video from, //build/ and several of the MS developers were using a syntax like this in their C++11 programs:
auto foo = ref new Foo();
I understand what everything does in this line except "ref". What does that mean?
The forthcoming Visual C++ compiler adds this syntax for dealing with WinRT objects (which are in turn the next generation of COM, what have we gone through now? COM, DCOM, COM+, ActiveX, ...)
That line is nearly equivalent to:
com_ptr_t<Foo> foo = CreateInstance<Foo>();
But there's a new version of com_ptr_t
as well, using the syntax Foo^
.
new
and delete
. –
Puri boost::shared_ptr
. com_ptr_t
already did that as well. Why they introduced a special syntax for something built into C++ I don't know. –
Puri AddRef
/ Release
. –
Bordie &
like ATL does and break STL. –
Bordie operator&
is no longer a violation of STL constraints. Secondly, APIs designed to be written with the new stuff could just take a com_ptr_t
. However, Herb Sutter said that they very nearly did make it library-only in C++, but there were some strange edge cases which produced very bad performance as a library and they had to make it language to solve them. The syntax enhancements themselves were already in the compiler for C++/CLI. –
Mell com_ptr_t
, but a portable ABI can't, unless you demand a very specific representation for the smart pointer (which is why e.g. you can't use ATL CComPtr<T>
in method signatures when writing COM components today, and have to take/return raw pointers). Granted, VC++ already lays out vtables in a very specific way for COM... –
Bordie "ref new" is a 2 token keyword. It instructs the compiler to instantiate a windows runtime object and automatically manage the lifetime of the object (via the "^" operator).
Instantiating a windows runtime object causes an allocation, but it does not have to be on the heap.
ref in this case stands for reference counting. Classes using ref are WinRT component which have reference count machanisms out of the box.
© 2022 - 2024 — McMap. All rights reserved.