Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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++_Regex_Macros_Preprocessor - Fatal编程技术网

C++ 类似宏的函数不接受嵌套的#定义

C++ 类似宏的函数不接受嵌套的#定义,c++,regex,macros,preprocessor,C++,Regex,Macros,Preprocessor,我正在使用宏根据模式确定变量的实际类型,并且我遇到了一些我不理解的奇怪的宏错误: a.cpp:15:4: error: '#' is not followed by a macro parameter #define USING_INTEGER\ ^ a.cpp:15:11: error: unknown type name 'USING_INTEGER' #define USING_INTEGER

我正在使用宏根据模式确定变量的实际类型,并且我遇到了一些我不理解的奇怪的宏错误:

a.cpp:15:4: error: '#' is not followed by a macro parameter
                #define USING_INTEGER\
                 ^
a.cpp:15:11: error: unknown type name 'USING_INTEGER'
                #define USING_INTEGER\
                        ^
a.cpp:16:2: error: expected unqualified-id
        else if (matchRegex(DOUBLE_REGEX, val))\
        ^
3 errors generated.
为什么会这样?我不知道,如果你知道原因,请提供一些帮助

#包括“示例3.cpp”
#包括
std::string INT_REGEX=“^[-+]?\\d+$”,
DOUBLE\u REGEX=“^[-+]?\\d+\\.\\d?$”,
布尔值_REGEX=“^(真|假)$”;
bool matchRegex(std::string模式,std::string inputString){
正则表达式(模式);
返回std::regex_匹配(inputString,expression);
}
#定义determineType(var)\
if(matchRegex(INT_REGEX,val))\
#使用_整数定义\
else if(匹配正则表达式(双正则表达式,val))\
#使用双精度定义\
else if(匹配正则表达式(布尔正则表达式,val))\
#用布尔定义\
否则\
#使用_RAW定义

据我所知,OP问题的动机是:

  • 解析文本
  • 确定某种类型的输入
  • 将值存储在该类型中
必须在编译时定义变量类型。 这是C++语言的核心概念之一。 在运行时没有机会定义变量的类型。)

因此,所有需要支持的类型必须同时在源代码中定义

作为输入项可能总是有最多一种类型,值的存储可以考虑。

在C中,A会出现在脑海中,但是C++提供了更好的东西: 变量将使用所有支持的类型定义,但实例将始终存储其中一个类型的值。根据右侧的不同,在分配中选择类型

举例说明在行动中:

#include <iostream>
#include <sstream>
#include <string>
#include <variant>

// an error type
struct None { std::string text; };

// a value what can represent one of all supported types
typedef std::variant<bool, int, double, std::string, None> Value;

// reads a value from a text determining its type
Value readInput(const std::string &text)
{
  // check for int
  { std::istringstream in(text); int value;
    if (in >> value && in.tellg() == -1) {
      return Value(value);
    }
  }
  // check for floating point
  { std::istringstream in(text); double value;
    if (in >> value && in.tellg() == -1) {
      return Value(value);
    }
  }
  // check for bool
  if (text == "true") return Value(true);
  if (text == "false") return Value(false);
  // check for (quoted) string
  if (text.size() >= 2
    && ((text.front() == '"' && text.back() == '"')
    || (text.front() == '\'' && text.back() == '\''))) {
    return Value(text.substr(1, text.size() - 2));
  }
  // ERROR
  return Value(None{ text });
}

// prints the value (considering the current type)
void print(const Value &value)
{
  switch (value.index()) {
    case 0: std::cout << "bool: " << std::boolalpha << std::get<bool>(value); break;
    case 1: std::cout << "int: " << std::get<int>(value); break;
    case 2: std::cout << "double: " << std::get<double>(value); break;
    case 3: std::cout << "string: '" << std::get<std::string>(value) << '\''; break;
    case 4: std::cout << "ERROR! text: '" << std::get<None>(value).text << '\''; break;
    default: std::cout << "Value not initialized.";
  }
}

int main()
{
  const std::string tests[] = {
    "true", // bool
    "false", // bool
    "123", // int
    "123.17", // double
    "0", // int
    "0.0", // double
    "'text'", // string
    "''", // string
    "something that doesn't match any type" // ERROR
  };
  for (const std::string &test : tests) {
    std::cout << "Test \"" << test << "\"\n";
    const Value value = readInput(test);
    std::cout << "Got: ";
    print(value);
    std::cout << '\n';
  }
}

你不能那样做。预处理器不允许在宏的中间定义符号。嵌套的<代码> >定义< /代码>不是一件事;编码> >根据C++代码的结果定义双重不可。你为什么要用宏来做这件事?@Shawn该死的糟透了,你知道另一种方法吗?@Ry我用它来设置变量类型的标志,以便以后使用-我正在为Scheme制作一个transpiler,我的Car函数返回一个字符串,但我需要一种正确的方法来生成一个正确类型的字符串-这似乎是合适的-记住预处理器有效地运行在一个过程和一个过程中。另外,不能使用C++<代码>如果代码有条件地<代码> >定义什么,你只能使用< C++ >代码> >代码> > IFNDEF 来操作它们。重要的是要注意,C++代码在执行程序启动之前并没有运行,这是编译结束后很久的事情。