mercredi 19 juillet 2023

template parameter type deduction misunderstanding

I have a template function with a universal (forwarding) parameter, as shown below:

#include <iostream>
#include <type_traits>

template <typename T>
void foo(T&& arg)
{
    // Check the type of T
    if constexpr (std::is_lvalue_reference_v<T>) {
        std::cout << "T is an lvalue reference" << std::endl;
    } else if constexpr (std::is_rvalue_reference_v<T>) {
        std::cout << "T is an rvalue reference" << std::endl;
    } else {
        std::cout << "T is neither lvalue nor rvalue reference" << std::endl;
    }
}

int main() 
{
    foo(10);
    return 0;
}

In this code, I have a function template foo with a universal (forwarding) reference parameter named arg. The purpose of this function is to check the type of the template parameter T.

In the main function, I call foo with the argument 10. Since 10 is an rvalue, I expect the result to be 'T is an rvalue reference.' However, when I compile and run this code in the "https://ift.tt/K2dR0I5" online compiler using the "g++ -std=c++17 -Wall -pthread -fno-elide-constructors -Qn main.cpp && ./a.out" flags, the actual output is 'T is neither lvalue nor rvalue reference.'

My assumption is that the template type T would be deduced as int&& here. But I guess the result is int which is similar to the behavior of decltype(10). Is the type deduction here the same as calling decltype(X)? Or is there any rule here?

moreover:

if call foo as below,

int x = 10;
foo(std::move(x));

the result is "T is neither lvalue nor rvalue reference" ? I expect "T is an rvalue reference"...

Aucun commentaire:

Enregistrer un commentaire