Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays and Rvalues (as parameters)

I wonder if there is any way to differentiate the function calls (with arrays as parameters) shown in the following code:

#include <cstring>
#include <iostream>

template <size_t Size>
void foo_array( const char (&data)[Size] )
{
    std::cout << "named\n";
}

template <size_t Size>
void foo_array( char (&&data)[Size] )  //rvalue of arrays?
{
    std::cout << "temporary\n";
}


struct A {};

void foo( const A& a )
{
    std::cout << "named\n";
}

void foo( A&& a )
{
    std::cout << "temporary\n";
}


int main( /* int argc, char* argv[] */ )
{
    A a;
    const A a2;

    foo(a);
    foo(A());               //Temporary -> OK!
    foo(a2);

    //------------------------------------------------------------

    char arr[] = "hello";
    const char arr2[] = "hello";

    foo_array(arr);
    foo_array("hello");     //How I can differentiate this?
    foo_array(arr2);

    return 0;
}

The foo "function family" is able to distinguish a temporary object from a named. Is not the case of foo_array.

Is it possible in C++11 ? If not, do you think could be possible? (obviously changing the standard)

Regards. Fernando.

like image 835
Fernando Pelliccioni Avatar asked May 16 '12 18:05

Fernando Pelliccioni


1 Answers

There is nothing wrong with foo_array. It's the test case that is bad: "hello" is an lvalue! Think about it. It is not a temporary: string literals have static storage duration.

An array rvalue would be something like this:

template <typename T>
using alias = T;
// you need this thing because char[23]{} is not valid...

foo_array(alias<char[23]> {});
like image 141
R. Martinho Fernandes Avatar answered Sep 22 '22 22:09

R. Martinho Fernandes