【问题标题】:XCode 9.3 upgrade or replace libc++XCode 9.3 升级或替换 libc++
【发布时间】:2018-04-06 14:53:15
【问题描述】:

我在 C++ 中有以下代码,它在 GCC、MSVC2015、Android NDK 上运行良好......

class JsonLongText {
private:
    enum JsonToken {
        JT_OBJECT,
        JT_ARRAY,
        JT_VARIABLE,
        JT_EMPTY
    };
    typedef std::vector<JsonToken> JsonTokenStack;
    JsonTokenStack                  m_OpenTokens;
....
    JsonLongText() : m_IgnoreEmptyItems(true), m_RequireItemSeparator(false), m_PrettyPrint(false) {}
}

我也成功地在 MacOS 10.13 上使用 XCode 9.2 从命令行编译了它。但是在我的系统从 10.13 升级到 10.13.4 和 Xcode 升级到 9.3 之后。我面临以下错误:

In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:643:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2141:9: error: no matching constructor for
      initialization of '__compressed_pair_elem<JsonLongText::JsonToken *, 0>'
      : _Base1(std::forward<_Tp>(__t)), _Base2() {}
        ^      ~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:423:7: note: in instantiation of function template
      specialization 'std::__1::__compressed_pair<JsonLongText::JsonToken *, std::__1::allocator<JsonLongText::JsonToken> >::__compressed_pair<long, true>'
      requested here
      __end_cap_(nullptr)
      ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:473:5: note: in instantiation of member function
      'std::__1::__vector_base<JsonLongText::JsonToken, std::__1::allocator<JsonLongText::JsonToken> >::__vector_base' requested here
    vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
    ^
../Include/JsonLongText.h:162:2: note: in instantiation of member function 'std::__1::vector<JsonLongText::JsonToken,
      std::__1::allocator<JsonLongText::JsonToken> >::vector' requested here
        JsonLongText() : m_IgnoreEmptyItems(true), m_RequireItemSeparator(false), m_PrettyPrint(false) {}
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2037:8: note: candidate constructor
      (the implicit copy constructor) not viable: no known conversion from 'long' to 'const std::__1::__compressed_pair_elem<JsonLongText::JsonToken *, 0,
      false>' for 1st argument
struct __compressed_pair_elem {
       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2037:8: note: candidate constructor
      (the implicit move constructor) not viable: no known conversion from 'long' to 'std::__1::__compressed_pair_elem<JsonLongText::JsonToken *, 0, false>'
      for 1st argument
struct __compressed_pair_elem {
       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2050:3: note: candidate template ignored:
      substitution failure [with _Up = long, $1 = void]
  __compressed_pair_elem(_Up&& __u)
  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2055:3: note: candidate constructor template not
      viable: requires 3 arguments, but 1 was provided
  __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2043:39: note: candidate constructor not viable:
      requires 0 arguments, but 1 was provided
  _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
                                      ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2141:9: error: no matching constructor for
      initialization of '__compressed_pair_elem<unsigned long *, 0>'
      : _Base1(std::forward<_Tp>(__t)), _Base2() {}
        ^      ~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:423:7: note: in instantiation of function template
      specialization 'std::__1::__compressed_pair<unsigned long *, std::__1::allocator<unsigned long> >::__compressed_pair<long, true>' requested here
      __end_cap_(nullptr)
      ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:473:5: note: in instantiation of member function
      'std::__1::__vector_base<unsigned long, std::__1::allocator<unsigned long> >::__vector_base' requested here
    vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
    ^
../Include/JsonLongText.h:162:2: note: in instantiation of member function 'std::__1::vector<unsigned long, std::__1::allocator<unsigned long> >::vector'
      requested here
        JsonLongText() : m_IgnoreEmptyItems(true), m_RequireItemSeparator(false), m_PrettyPrint(false) {}
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2037:8: note: candidate constructor
      (the implicit copy constructor) not viable: no known conversion from 'long' to 'const std::__1::__compressed_pair_elem<unsigned long *, 0, false>' for
      1st argument
struct __compressed_pair_elem {
       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2037:8: note: candidate constructor
      (the implicit move constructor) not viable: no known conversion from 'long' to 'std::__1::__compressed_pair_elem<unsigned long *, 0, false>' for 1st
      argument
struct __compressed_pair_elem {
       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2050:3: note: candidate template ignored:
      substitution failure [with _Up = long, $1 = void]
  __compressed_pair_elem(_Up&& __u)
  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2055:3: note: candidate constructor template not
      viable: requires 3 arguments, but 1 was provided
  __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2043:39: note: candidate constructor not viable:
      requires 0 arguments, but 1 was provided
  _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}

我尝试将整个目录 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1 替换为来自 XCode 9.2 的另一台 Mac 的文件。有了这些文件,我的项目的构建就成功完成了。当前版本的 XCode 中的 libc++ 似乎已损坏。

那么,是否可以降级 XCode 版本或升级 libc++?


编辑: 尝试使用正则表达式时会出现类似的错误。以下代码(从 cppreference 复制):

// Simple regular expression matching
std::string fnames[] = {"foo.txt", "bar.txt", "baz.dat", "zoidberg"};
std::regex txt_regex("[a-z]+\\.txt");

for (const auto &fname : fnames) {
    std::cout << fname << ": " << std::regex_match(fname, txt_regex) << '\n';
} 

编译命令:

g++ -std=c++11 -DSTATIC_LIB -g -I . -I ../../../../openssl/iOS/include -o out/Expression.o -c Expression.cpp

原因:

In file included from Expression.cpp:113:
In file included from ./StdAfx.h:23:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:643:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2051:9: error: cannot initialize a member subobject
      of type 'std::__1::sub_match<const char *> *' with an rvalue of type 'long'
      : __value_(_VSTD::forward<_Up>(__u)){};
        ^        ~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2151:9: note: in instantiation of function template
      specialization 'std::__1::__compressed_pair_elem<std::__1::sub_match<const char *> *, 0, false>::__compressed_pair_elem<long, void>' requested here
      : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:432:7: note: in instantiation of function template
      specialization 'std::__1::__compressed_pair<std::__1::sub_match<const char *> *, std::__1::allocator<std::__1::sub_match<const char *> >
      >::__compressed_pair<long, const std::__1::allocator<std::__1::sub_match<const char *> > &>' requested here
      __end_cap_(nullptr, __a)
      ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:485:11: note: in instantiation of member function
      'std::__1::__vector_base<std::__1::sub_match<const char *>, std::__1::allocator<std::__1::sub_match<const char *> > >::__vector_base' requested here
        : __base(__a)
          ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:5355:7: note: in instantiation of member function
      'std::__1::vector<std::__1::sub_match<const char *>, std::__1::allocator<std::__1::sub_match<const char *> > >::vector' requested here
    : __matches_(__a),
      ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2988:34: note: in instantiation of member function
      'std::__1::match_results<const char *, std::__1::allocator<std::__1::sub_match<const char *> > >::match_results' requested here
    match_results<const _CharT*> __m;
                                 ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2978:5: note: (skipping 4 contexts in backtrace; use
      -ftemplate-backtrace-limit=0 to see all)
    __lookahead(const basic_regex<_CharT, _Traits>& __exp, bool __invert, __node<_CharT>* __s, unsigned __mexp)
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:4098:35: note: in instantiation of function template
      specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_term<const char *>' requested here
        _ForwardIterator __temp = __parse_term(__first, __last);
                                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:4074:31: note: in instantiation of function template
      specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_alternative<const char *>' requested here
    _ForwardIterator __temp = __parse_alternative(__first, __last);
                              ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:3026:19: note: in instantiation of function template
      specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_ecma_exp<const char *>' requested here
        __first = __parse_ecma_exp(__first, __last);
                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2522:10: note: in instantiation of function template
      specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse<const char *>' requested here
        {__parse(__p, __p + __traits_.length(__p));}
         ^
Expression.cpp:3356:16: note: in instantiation of member function 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::basic_regex' requested here
    std::regex txt_regex("[a-z]+\\.txt");
               ^
In file included from Expression.cpp:113:
In file included from ./StdAfx.h:23:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:643:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2051:9: error: cannot initialize a member subobject
      of type 'std::__1::__state<char> *' with an rvalue of type 'long'
      : __value_(_VSTD::forward<_Up>(__u)){};
        ^        ~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2141:9: note: in instantiation of function template
      specialization 'std::__1::__compressed_pair_elem<std::__1::__state<char> *, 0, false>::__compressed_pair_elem<long, void>' requested here
      : _Base1(std::forward<_Tp>(__t)), _Base2() {}
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:423:7: note: in instantiation of function template
      specialization 'std::__1::__compressed_pair<std::__1::__state<char> *, std::__1::allocator<std::__1::__state<char> > >::__compressed_pair<long, true>'
      requested here
      __end_cap_(nullptr)
      ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:473:5: note: in instantiation of member function
      'std::__1::__vector_base<std::__1::__state<char>, std::__1::allocator<std::__1::__state<char> > >::__vector_base' requested here
    vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:5536:21: note: in instantiation of member function
      'std::__1::vector<std::__1::__state<char>, std::__1::allocator<std::__1::__state<char> > >::vector' requested here
    vector<__state> __states;
                    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2990:29: note: in instantiation of function template
      specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__match_at_start_ecma<std::__1::allocator<std::__1::sub_match<const char *>
      > >' requested here
    bool __matched = __exp_.__match_at_start_ecma(
                            ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2978:5: note: (skipping 4 contexts in backtrace; use
      -ftemplate-backtrace-limit=0 to see all)
    __lookahead(const basic_regex<_CharT, _Traits>& __exp, bool __invert, __node<_CharT>* __s, unsigned __mexp)
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:4098:35: note: in instantiation of function template
      specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_term<const char *>' requested here
        _ForwardIterator __temp = __parse_term(__first, __last);
                                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:4074:31: note: in instantiation of function template
      specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_alternative<const char *>' requested here
    _ForwardIterator __temp = __parse_alternative(__first, __last);
                              ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:3026:19: note: in instantiation of function template
      specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_ecma_exp<const char *>' requested here
        __first = __parse_ecma_exp(__first, __last);
                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2522:10: note: in instantiation of function template
      specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse<const char *>' requested here
        {__parse(__p, __p + __traits_.length(__p));}
         ^
Expression.cpp:3356:16: note: in instantiation of member function 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::basic_regex' requested here
    std::regex txt_regex("[a-z]+\\.txt");
               ^
In file included from Expression.cpp:113:
In file included from ./StdAfx.h:23:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:643:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2141:9: error: no matching constructor for
      initialization of '__compressed_pair_elem<std::__1::sub_match<const char *> *, 0>'
      : _Base1(std::forward<_Tp>(__t)), _Base2() {}
        ^      ~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:423:7: note: in instantiation of function template
      specialization 'std::__1::__compressed_pair<std::__1::sub_match<const char *> *, std::__1::allocator<std::__1::sub_match<const char *> >
      >::__compressed_pair<long, true>' requested here
      __end_cap_(nullptr)
      ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:473:5: note: in instantiation of member function
      'std::__1::__vector_base<std::__1::sub_match<const char *>, std::__1::allocator<std::__1::sub_match<const char *> > >::__vector_base' requested here
    vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:1326:5: note: in instantiation of member function
      'std::__1::vector<std::__1::sub_match<const char *>, std::__1::allocator<std::__1::sub_match<const char *> > >::vector' requested here
    __state()
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:5545:28: note: in instantiation of member function
      'std::__1::__state<char>::__state' requested here
        __states.push_back(__state());
                           ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2990:29: note: in instantiation of function template
      specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__match_at_start_ecma<std::__1::allocator<std::__1::sub_match<const char *>
      > >' requested here
    bool __matched = __exp_.__match_at_start_ecma(
                            ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2978:5: note: (skipping 4 contexts in backtrace; use
      -ftemplate-backtrace-limit=0 to see all)
    __lookahead(const basic_regex<_CharT, _Traits>& __exp, bool __invert, __node<_CharT>* __s, unsigned __mexp)
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:4098:35: note: in instantiation of function template
      specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_term<const char *>' requested here
        _ForwardIterator __temp = __parse_term(__first, __last);
                                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:4074:31: note: in instantiation of function template
      specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_alternative<const char *>' requested here
    _ForwardIterator __temp = __parse_alternative(__first, __last);
                              ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:3026:19: note: in instantiation of function template
      specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_ecma_exp<const char *>' requested here
        __first = __parse_ecma_exp(__first, __last);
                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2522:10: note: in instantiation of function template
      specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse<const char *>' requested here
        {__parse(__p, __p + __traits_.length(__p));}
         ^
Expression.cpp:3356:16: note: in instantiation of member function 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::basic_regex' requested here
    std::regex txt_regex("[a-z]+\\.txt");
               ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2037:8: note: candidate constructor
      (the implicit copy constructor) not viable: no known conversion from 'long' to 'const std::__1::__compressed_pair_elem<std::__1::sub_match<const char
      *> *, 0, false>' for 1st argument
struct __compressed_pair_elem {
       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2037:8: note: candidate constructor
      (the implicit move constructor) not viable: no known conversion from 'long' to 'std::__1::__compressed_pair_elem<std::__1::sub_match<const char *> *,
      0, false>' for 1st argument
struct __compressed_pair_elem {
       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2050:3: note: candidate template ignored:
      substitution failure [with _Up = long, $1 = void]
  __compressed_pair_elem(_Up&& __u)
  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2055:3: note: candidate constructor template not
      viable: requires 3 arguments, but 1 was provided
  __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2043:39: note: candidate constructor not viable:
      requires 0 arguments, but 1 was provided
  _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}

【问题讨论】:

  • 根据您的编译方式,您可能需要允许 xcode 重新安装命令行工具。
  • 我用正则表达式添加了更好的例子,因为上传整个类 JsonLongText 几乎是不可能的。
  • @Richard Hodges:我已经重新安装了命令行工具,但没有帮助。
  • 您可以尝试使用 Clang 代替 GCC,并将 -stdlib=libc++ 添加到您的构建命令行吗?
  • libstdc++ 是 C++98 的 GNU C++ 标准库,它不支持 C++11。您确定不想使用 libc++,它是支持 C++11/14/17 的 LLVM C++ 标准库吗?

标签: c++ ios xcode macos libc++


【解决方案1】:

我现在可以在我的机器上逐字重现您的确切 regex 错误。

诀窍是将nullptr 重新定义为0u 作为预处理器符号,然后包含vector 标准库头(regex 依赖于):

#define nullptr 0l

#include <regex>
#include <iostream>

int main ()
{
    std::string fnames[] = {"foo.txt", "bar.txt", "baz.dat", "zoidberg"};
    std::regex txt_regex("[a-z]+\\.txt");

    for (const auto &fname : fnames)
        std::cout << fname << ": " << std::regex_match(fname, txt_regex) << '\n';
}

鉴于上述情况,我现在认为您的问题是标头预处理器命名空间污染的情况:某些标头,在您失败的翻译单元的包含链中的某处,可能以这种方式定义了 nullptr 宏,这会导致错误使用 Xcode 9.3 附带的标准库的实现,但(我推测)由于某种原因没有使用其他实现。

我建议使用g++ -E 检查预处理器输出,并检查/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:432 中的违规代码的结果,我认为错误最初是在此处触发的。

或者,您可以尝试始终在翻译单元中首先包含vector。我怀疑这可以让您在继续寻找根本原因的同时避开问题。

祝你好运!

【讨论】:

  • 非常感谢。我以前的同事因为 MSVC2010 而真正定义了nullptr 8 年前。不幸的是,他只用#if (_MSC_VER &lt; 1600) 保护它。他忘了检查_WIN32
【解决方案2】:

我刚遇到问题,xcode版本12.4,仔细查看我的代码,发现有一个define:#define nullptr(NULL)。去掉这段代码,再次构建成功。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    相关资源
    最近更新 更多