Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,
My Question may quite simple or unsolvable.
Say we define two function (as simple as possible) as below :

C++
void foo(int&& arg){
 ...
}
 void bar(const int* x){
 ...
}


we all know we can simply call "bar" function just using rvalue of int-
C++
foo(47);

but problem arise when we trying to invoke bar function using rvalue type of a array-
C++
bar({1,2,3});

argument is treated as std::initilizer_list by compiler,so How can we solve this particular problem?
Can't we construct a r-value of a array due to some run-time conflicts?
Please explain.
Thank you.
Posted
Updated 25-Mar-15 6:31am
v3

1 solution

Array rvalues can be constructed by accessing array members of a class rvalues or by using an identity template:

C++
#include <iostream>

void f(int (&&x)[2][3]) { std::cout << sizeof x << '\n'; }

struct X { int i[2][3]; } x;
template<typename T> using identity = T;

int main()
{
    f(X().i); // binds to rvalue
    f(identity<int[][3]>{{1,2,3},{4,5,6}}); // binds to rvalue
}


Why not just accept initializer lists though?
 
Share this answer
 
v2
Comments
Buddhi Chaturanga 25-Mar-15 21:55pm    
@CubbiMew - Thanks for the quick reply pal,I have tried that already but the problem is we have to use template and that kind of things and specify the type of the element.
Imagine we have set of overloaded functions(only C++) regarding "bar" say,
bar(double* d_arg){...};
bar(int* i_arg){...};
then we call each functions only using curly brackets without specifying respective type,
bar({1,2,3}) expect to invoke bar(int*)
bar({1.0,2.0,3.0}) will invoke bar(double*) respectively.
(Note that compiler should aware of type of function argument)
CubbiMew 25-Mar-15 22:11pm    
So you're not actually looking for array rvalues? If I understood the actual problem, you just need to write a function template overload for std::initializer_lists, and have it call your pointer-taking function template with the result of calling begin on its parameter.

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