Skip to content
败犬日报 2026-01-16

败犬日报 2026-01-16

1. 左值隐式转换后匹配到右值引用

这是一道 cppquiz

cpp
#include <iostream>

void f(float &&) { std::cout << "f"; }
void f(int &&) { std::cout << "i"; }

template <typename... T>
void g(T &&... v)
{
    (f(v), ...);
}

int main()
{
    g(1.0f, 2);
}

答案是输出 if,非常反直觉。

参数包展开为 (f(v_0), f(v_1)); 这里从左到右求值没问题。关键在于 v 是具名的,是个左值,左值不能匹配右值引用,例如:

cpp
void f(int&&);
int&& v = 114;
f(v);  // Rvalue reference to type 'int' cannot bind to lvalue of type 'int'

回到原问题,float 左值把 f(float&&) 的候选给移除了,在它视角下只有 f(int&&) 一个候选,只能先隐式转换成 int 纯右值,所以先输出了 "i"。另一边也同理,输出 "f"。

2. C++26 反射已合入 GCC 主线

望周知。