Explicitely passing a parameter by reference in C++03 In C#, passing a parameter by reference can't be ignored:

// C#
void foo(ref int p_value) ; // passing an int by reference

void bar()
{
   int i = 0 ;
   foo(ref i) ;
}


In C++ (and as far as I am concerned, it is a good thing), the passing by reference is only marked in the function prototype, so the code above becomes:

// C++
void foo(int &amp; p_value) ; // passing an int by reference

void bar()
{
   int i = 0 ;
   foo(i) ;     // at call site, there is no "ref" marker.
}


The thing is, for a few special functions, to ease code reviewing, we would want to use a C#-like solution to read at call site if a variable is expected to be modified or not.

I devised a solution wrapping the value with a struct, something like:

template&lt;typename T&gt;
struct Ref
{
   T &amp; m_value ;
   explicit Ref(T &amp; p_value) : m_value(p_value) {}

   operator T &amp; () { return this-&gt;m_value ; }
   operator const T &amp; () const { return this-&gt;m_value ; }
} ;

template &lt;typename T&gt;   // wrapping T with Ref
Ref&lt;T&gt; byRef(T &amp; p_value)
{
   return Ref&lt;T&gt;(p_value) ;
}

template &lt;typename T&gt;   // idempotent-enabling overload (no need for Ref&lt;Ref&lt;T&gt;&gt;)
Ref&lt;T&gt; &amp; byRef(Ref&lt;T&gt; &amp; p_value)
{
   return p_value ;
}


I expect this trivial code to be inlined away in optimized code.

This enables me to write the code:

void foo(Ref&lt;int&gt; p_int)
{
   p_int *= 10 ;
}

void bar()
{
   int i = 0 ;
   foo(byRef(i)) ;
}


Which is exactly what I want:


no use of foo(&amp; i) C-style notation, with pointers, needing to handle NULL, etc.
in the cases we care, we can see in the code what is passed by non-const reference


Did I miss some unexpected problem/hidden cost that will come back to bite me in a few weeks/months/years?

Could this code be written better?

Will the code be confusing when seen through some un*x debuggers?

P.S.: Of course, Ref and byRef will be properly namespaced. I'm still not sure about making Ref::m_value public or private.
I can't see anything I would do majorly different.


I would make the member T &amp; m_value private.  
I would change the name of the method byRef() to just ref()

Note you still have a difference in identifiers.
The class is Ref while the function is ref()



The only problem I see is that at inside the function, the object is not automatically usable in its native type you need to cast it back before you can use it:

struct X { void print();};

void print(Ref&lt;X&gt; x)
{
    x.print();  // This fails to compile

    // To use the x object I need to cast it back to an X
    static_cast&lt;X&amp;&gt;(x).print();

    // If we are going to use it multiple times then we could set up
    // a local reference to the object as this will generate an implicit cast
    X&amp;  x1 = x;
    x1.print();
}


Thus getting at methods in the object becomes slightly on the messy side.