Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 什么是布尔<;正确>;在C++;-是boost公司的吗?_C++ - Fatal编程技术网

C++ 什么是布尔<;正确>;在C++;-是boost公司的吗?

C++ 什么是布尔<;正确>;在C++;-是boost公司的吗?,c++,C++,我试图使用一些示例代码,但我的编译器不会编译这一行: static void exitActions(Host& h, Bool<true>) {} 样品来自中国。代码只是文本中的片段——没有关于“包含”内容的片段——很难理解。Bool模板没有定义。我猜Bool的定义如下 template <bool B> struct Bool{}; 模板结构Bool{}; 您可以将其用于一些基本的模式匹配: void exitActions(Bool<true&g

我试图使用一些示例代码,但我的编译器不会编译这一行:

static void exitActions(Host& h, Bool<true>) {}

样品来自中国。代码只是文本中的片段——没有关于“包含”内容的片段——很难理解。Bool模板没有定义。

我猜
Bool
的定义如下

template <bool B> struct Bool{};
模板结构Bool{};
您可以将其用于一些基本的模式匹配:

void exitActions(Bool<true>)  { std::cout << "called with true\n"; }
void exitActions(Bool<false>) { std::cout << "called with false\n"; }

int main()
{
  exitActions(Bool<true>());  // prints "called with true"
  exitActions(Bool<false>()); // prints "called with false"
}

无效退出(Bool){std::cout-Bool是一种类型,基于此类型,激发可能会过载。您能否将看到的错误消息添加到问题中?嗯,
exitActions
中对Bool参数的任何引用都可能会出错。如果您将所有引用更改为type
Bool
,您可能看不到hings崩溃了,但在一些更聪明的答案出现之前,请将其视为初步答案。当您将光标放在
Bool
上并使用“转到定义”时会发生什么?示例代码是从哪里获得的?它不是从Boost获得的,它的等价物是
Boost::true_type
/
Boost::integral_constant
。我假设这是标记分派的一个实例--您可以查找该技术。使用此定义可以编译程序,谢谢。这种类型的程序有名字吗NG或Web链接。它像一个重载函数?一个或另一个被调用取决于你是否通过真或假?我猜,正如你所说的,在其他代码的上下文中,它确实是有意义的。我将研究更多。我以前从未在C++中看到过(在其他编程语言中,这被称为模式匹配,如已写的)。。通常您会编写类似于
模板void exitctions(){…}
模板void exitctions(){…}
的内容。但是,由于经常使用这种方法,他可能会决定通过引入这种
Bool
类型来缩短这种情况。
void exitActions(Bool<true>)  { std::cout << "called with true\n"; }
void exitActions(Bool<false>) { std::cout << "called with false\n"; }

int main()
{
  exitActions(Bool<true>());  // prints "called with true"
  exitActions(Bool<false>()); // prints "called with false"
}
Tran<T,S,T>::entryActions(host_, Bool<false>());