我一直在阅读有关SFINAE的内容,并查看以下某些变体的一些示例:
"C:\Program Files (x86)\JetBrains\WebStorm 2016.1.2\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" --debug-brk=22714 my-lib.spec.js
Debugger listening on port 22714
c:\Users\<me>\WebstormProjects\my-lib\spec\my-lib.spec.js:4
describe("Scenario", () => {
^
ReferenceError: describe is not defined
at Object.<anonymous> (c:\Users\<me>\WebstormProjects\my-lib\spec\<my-lib>.js:4:1)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.runMain [as _onTimeout] (module.js:442:10)
at Timer.listOnTimeout (timers.js:92:15)
Process finished with exit code 1
(取自https://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error)
我对#include <iostream>
#include <type_traits>
template <typename... Ts> using void_t = void;
template <typename T, typename = void>
struct has_typedef_foobar : std::false_type {};
template <typename T>
struct has_typedef_foobar<T, void_t<typename T::foobar>> : std::true_type {};
struct foo {
using foobar = float;
};
int main() {
std::cout << std::boolalpha;
std::cout << has_typedef_foobar<int>::value << std::endl;
std::cout << has_typedef_foobar<foo>::value << std::endl;
}
成员的来源感到困惑。 has_typedef_foobar的两个定义似乎都没有指定名为value的布尔成员。
value
在哪里获得价值?我怀疑它是某种编译器提供的值,并且想要阅读它,但我不确定谷歌的术语,因为我的查询带来了其他与C ++ 11相关的价值相关主题。
感谢。
答案 0 :(得分:7)
std::true_type
和std::false_type
定义为:
using true_type = std::integral_constant<bool, true>
using false_type = std::integral_constant<bool, false>
分别。也就是说,它们是std::integral_constant
的两个独立实例。
现在,如果你看一下std::integeral_constant
的可能实现:
template<class T, T v>
struct integral_constant {
...
static constexpr T value = v;
...
};
除此之外,您还会看到名为static constexpr
的{{1}}变量。当然,如果您将value
实例化为std::integeral_constant
,那么instatiatation中的std::integral_constant<bool, true>
成员变量将设置为value
。以同样的方式,如果您将true
实例化为std::integral_constant
,则其std::integral_constant<bool, false>
成员变量将设置为value
。
从false
继承您还继承了设置为std::true_type
并继承自value
类型的true
成员变量,您还继承了std::false_type
成员变量设置为value
。