为什么 std::regex 不是有效类型?

Why is std::regex not a valid type?

假设我有以下代码:
class.h

#ifndef CLASS_H
#define CLASS_H

class Class
{
    private:
        std::regex* regex;
};

#endif

class.cpp

#include <regex>
#include "class.h"
// ...

编译会导致以下错误:

error: "regex" in namespace "std" does not name a type
    std::regex* regex;
         ^~~~~

但是我能够以任何其他方式使用 std::regex 库吗? 运行 在 GCC 6.1.1 上。还尝试使用 -std=C++11 标志显式编译。

#ifndef CLASS_H
#define CLASS_H

#include <regex>

class Class
{
    private:
        std::regex* regex;
};

#endif

如果您确实将库包含在 class 中,则工作正常。

Example

要使用 class,您有几种方法: 第一种方法是在首次使用前包含 class 的 header 文件。 如果您不想包含 header,您可以使用 forward declaration,但对于 std classes,它可能会导致未定义的行为。 下面是对 std classes 使用前向声明的示例: Forward declare an STL container?