Click here to Skip to main content
15,867,937 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm not familiar with C++ language features ( updated almost every year). There is a piece of code shows something about template reference but with two & characters. For one, It mainly means the reference of the object, but two ?
Here the code:
C++
Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(...);

template<typename TDelegateInterface, typename TLambda>
ComPtr<typename Details::DelegateArgTraitsHelper<TDelegateInterface>::Interface> Callback(TLambda&& callback) throw()
{
    using DelegateHelper = Details::DelegateArgTraitsHelper<TDelegateInterface>;
    return DelegateHelper::Traits::Callback<TDelegateInterface, DelegateHelper::Interface>(Details::Forward<TLambda>(callback));
}

    template<class T>
    struct Identity
    {
        // Map T to type unchanged
        typedef T Type;
    };

    template<class T> 
    inline T&& Forward(typename Identity<T>::Type& arg) throw()
    {
        // Forward arg, given explicitly specified Type parameter
        return (T&&)arg;
    }

inline T&& Forward(typename Identity<T>::Type& arg) throw()
{
    // Forward arg, given explicitly specified Type parameter
    return (T&&)arg;
}
It sounds like a template function , to me. But the return result , (T&&)arg .
What does it mean? What the exactly difference between one & and two & ?

What I have tried:

Sorry, nothing tried yet. This is some code from a tutorial about how to write webview2 with win32.
Callback should be a template function with an function parameter. The caller passed an anonymous function which is a lambda function(I use ... refer). Forward seems like to make an explicit convert of the function reference, but why two &?
what is the difference with C callback function pointer?
Can this code be written by C callback function pointer style?
Posted
Updated 12-Feb-23 1:17am
v3

1 solution

It is what is known as an rvalue reference. For a complete explanation see http://thbecker.net/articles/rvalue_references/section_01.html[^].
 
Share this answer
 
Comments
CPallini 12-Feb-23 14:02pm    
5.
Yount_0701 12-Feb-23 21:00pm    
Thank you. I think I have got some of it.
Webview2 using COM tech, pre-provide interface to access local components.
The implement of invoke interfaces / virtual classes make codes massive. On another hand, constructor may cause serious memory expense during value passing ( we could avoid this by using pointer but need to manage the release of object pointer, not convenient), especially in this case , the web browser component which would be expensive for sure. rvalue reference make sense.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900