相关疑难解决方法(0)

为什么我不能在C++中使用转发引用的特征?

我有以下测试代码.

有关可执行示例,请参阅godbolt https://godbolt.org/z/fLRM8d

template <typename T> struct Traits {
    static const bool value = false;
};

struct Zip{};
template <> struct Traits<Zip> {
    static const bool value = true;
};

template <typename E>
void Execute(E && e){
    static_assert(Traits<E>::value);
}

int main(){

    auto z = Zip();

    // Fails the static assertion with an lvalue
    Execute(z);

    // Passes the static assertion with an rvalue
    Execute(Zip());
}
Run Code Online (Sandbox Code Playgroud)

这里发生了什么,我不能像我期望的那样使用我的类型特征?建模这个问题的正确方法是什么?

c++ type-traits perfect-forwarding c++11

6
推荐指数
1
解决办法
116
查看次数

标签 统计

c++ ×1

c++11 ×1

perfect-forwarding ×1

type-traits ×1