相关疑难解决方法(0)

使`std :: get`与SFINAE一起玩得很好

std::get 似乎不是SFINAE友好的,如下面的测试用例所示:

template <class T, class C>
auto foo(C &c) -> decltype(std::get<T>(c)) {
    return std::get<T>(c);
}

template <class>
void foo(...) { }

int main() {
    std::tuple<int> tuple{42};

    foo<int>(tuple);    // Works fine
    foo<double>(tuple); // Crashes and burns
}
Run Code Online (Sandbox Code Playgroud)

在Coliru上看到它

目标是将第二次呼叫foo转向第二次超载.在实践中,libstdc ++给出:

/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.3.0/../../../../include/c++/6.3.0/tuple:1290:14: fatal error: no matching function for call to '__get_helper2'
    { return std::__get_helper2<_Tp>(__t); }
             ^~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

libc ++更直接,直接static_assert引爆:

/usr/include/c++/v1/tuple:801:5: fatal error: static_assert failed "type not found in type list"
    static_assert ( value != -1, "type not found …
Run Code Online (Sandbox Code Playgroud)

c++ sfinae language-lawyer stdtuple c++14

16
推荐指数
2
解决办法
675
查看次数

标签 统计

c++ ×1

c++14 ×1

language-lawyer ×1

sfinae ×1

stdtuple ×1