PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 4, 2022

[FIXED] Why universal reference as an input parameter doesn't work

 July 04, 2022     c++, c++17, pass-by-reference, rvalue-reference     No comments   

Issue

template<typename T>
constexpr auto log_value(T&& value) {
    if constexpr (std::is_enum_v<T>) {
        cout << "enum" << endl;
    }
    else {
        cout << "normal" << endl;
    }
}

I have a function to judge whether some value is an enum, and I test it by

enum class A {
    a,
    s,
    d
};

int main() {
    auto s = A::a;
    const auto& s1 = s;
    auto&& s2 = A::a;
    log_value(s);
    log_value(s1);
    log_value(s2);
    log_value(A::a);

But the result is

normal
normal
normal
enum

When I change it into:

template<typename T>
constexpr auto log_value_const_left(const T& value) {
    if constexpr (std::is_enum_v<T>) {
        cout << "enum" << endl;
    }
    else {
        cout << "normal" << endl;
    }
}

It works

enum
enum
enum
enum

Now I just wonder why log_value doesn't work? Isn't the input parameter T&& value in log_value called universal reference which could refer to either lvalue reference or rvalue reference?

And if std::is_enum_v<T> != true in log_value,what is it exactly? Why it's changed anyway?


Solution

The way forwarding (aka universal) references work for lvalues is by deducing the referenced type (T in your case) to an lvalue reference (A & in your case). Then T && also becomes A & according to the reference collapsing rules (& + && = &).

For rvalues this is not needed, and T is deduced to a non-reference.

You want std::is_enum_v<std::remove_reference_t<T>>.



Answered By - HolyBlackCat
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing