This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: std::ref( int ) gives double& ?


On 7 April 2013 23:59, michel wrote:
> hello,
> I thought std::ref(int) would become something like int& but it seems
> double&...If any one could explain the why std:ref(int)seems to become a
> double& somehow in the example below, thank you.
> Here is the test code, command line, and output (Using g++ 4.7.2):
>
> #include <iostream>
> #include <functional>
>
> using namespace std;
> template <typename T> void foo_impl(T val, true_type)
> {
>     cout << "someone interested by integral values "<<val<<endl;
> }
>
> template <typename T> void foo_impl(T val, false_type)
> {
>     cout << "someone interested by floating values "<<val<<endl;
> }
> template <typename T> void foo_impl(T val)
> {
>     foo_impl(val, std::is_integral<T>());
> }
>
> int main()
> {
>     int x = 12;
>     foo_impl(x);
>     foo_impl(std::ref(x));
> }
>
> michel@PC:$ g++ -std=c++11 -Wall -Wextra  wrapper.cpp
> michel@PC:$./a.out
> someone interested by integral values 12
> someone interested by floating values 12

You seem to believe that everything that isn't an integral type is a
double, which is quite obviously not true.

std::ref(x) returns std::reference_wrapper<int> which is a class type
so std::is_integral<std::reference_wrapper<int>> is false.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]