在boost::spirit::x3中用作标记的奇怪构造
Strange construct that used as tag in boost::spirit::x3
以下上下文中奇怪的语言结构X< class Y > Y;
是什么?
#include <iostream>
#include <sstream>
#include <typeinfo>
#include <type_traits>
#include <cstdlib>
#include <cxxabi.h>
template< typename T >
std::string const type_info_str()
{
int status = 0;
auto realname_(abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status));
switch (status) {
case -1: return "Could not allocate memory";
case -2: return "Invalid name under the C++ ABI mangling rules";
case -3: return "Invalid argument to demangle";
}
std::ostringstream oss;
if (std::is_volatile< T >::value) {
oss << "volatile ";
}
oss << realname_;
std::free(realname_);
if (std::is_const< T >::value) {
oss << " const";
}
if (std::is_rvalue_reference< T >::value) {
oss << " &&";
} else if (std::is_lvalue_reference< T >::value) {
oss << " &";
}
return oss.str();
}
template< typename T >
struct X { };
int main()
{
X< class Y > Y;
std::cout << type_info_str< decltype(Y) >() << std::endl; // X<main::Y>
return EXIT_SUCCESS;
}
它的目的和语义是什么?
让我们来分解一下:
X <class Y> Y
| ^^^^^^^^^ |
| | |
| | |
| | ---> name of the variable
| |
| ---> the struct is a template which is instantiated with a type
| (in this case, the type is an incomplete class named Y)
|
---> struct declared globally
正如Agent_L所提到的,类名和变量之间没有冲突,因为可以从上下文中推断出真正的含义:类型或变量
相关文章:
- Boost Spirit,获取迭代器内部语义动作
- boost::spirit::karma 替代生成器,带有 boost::variant 由字符串和字符串别名组成
- boost::spirit::x3 中的通用解析器生成器
- Boost.Spirit将表达转换为AST
- 使用 Boost.Spirit 解析具有混合数据类型的 OBJ 文件?
- 如何处理Boost Spirit X3导致Visual Studio 2019 "static initialization order fiasco"?
- Boost Spirit X3:将(一些)空格解析为枚举
- Boost Spirit x3 条件(三元)运算符解析器
- 你如何从 Boost Spirit X3 词法解析器中获取字符串?
- 将 Boost.Spirit.X3 解析器拆分为多个 TU
- boost::spirit::x3 中的简单字符串解析器不起作用
- boost::spirit指针属性是用nullptr初始化的吗?
- Boost.Spirit Alternative Parser parallelization
- 如何在 boost::spirit::qi 中将某些语义操作排除在 AST 之外
- 自定义预期失败的完整错误消息(boost::spirit::x3)
- boost::spirit--试图编译大多数简单代码的编译器错误
- Boost.Spirit.Qi 语法,用于 Boost.Fusion 自适应结构中的默认值
- 使用 Boost.Spirit 解析嵌套列表
- C++Spirit Boost:将输入迭代器转换为前向迭代器
- Spirit Boost real_p解析器行为奇怪