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.
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]> {});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With