error: implicit instantiation of undefined template 'llvm::yaml::MissingTrait

error: implicit instantiation of undefined template 'llvm::yaml::MissingTrait

我正在开发一个使用 LLVM YAML I/O 库的项目。这是我关注的documentation/tutorial:

我正在尝试复制您在 llvm::yaml::MappingTraits 上为 struct 数据类型定义专业化的示例。此示例位于页面顶部。

这是我编写的代码:

#include <cstdlib>  /* for EXIT_FAILURE */
#include <string>
#include <vector>

#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/YAMLParser.h"

using std::string;
using std::vector;

using llvm::outs;
using llvm::errs;

using llvm::yaml::ScalarEnumerationTraits;
using llvm::yaml::MappingTraits;
using llvm::yaml::IO;
using llvm::yaml::Input;
using llvm::yaml::Output;

struct Person {
    string name;
    int hatSize;
};

template <>
struct MappingTraits<Person> {
    static void mapping(IO& io, Person& info) {
        io.mapRequired("name", info.name);
        io.mapOptional("hat-size", info.hatSize);
    }
};

int main(int argc, const char **argv) {
    Person tom;
    tom.name = "Tom";
    tom.hatSize = 8;
    Person dan;
    dan.name = "Dan";
    dan.hatSize = 7;
    std::vector<Person> persons;
    persons.push_back(tom);
    persons.push_back(dan);

    Output yout(llvm::outs());
    yout << persons;

    return EXIT_SUCCESS;
}

在我看来,我已经完全复制了他们在该教程中的示例代码。但是当我尝试编译程序(使用 makefile)时,我收到了这个神秘的错误消息:

clang++ -I/usr/local/include -std=c++11   -fno-exceptions -fno-rtti -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -std=c++14 -fcxx-exceptions -g -Wall   -c -o yaml_project.o yaml_project.cpp
In file included from yaml_project.cpp:12:
/usr/local/include/llvm/Support/YAMLTraits.h:1871:36: error: implicit instantiation of undefined template 'llvm::yaml::MissingTrait<std::vector<Person, std::allocator<Person> > >'
  char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
                                   ^
yaml_project.cpp:153:10: note: in instantiation of function template specialization 'llvm::yaml::operator<<<std::vector<Person, std::allocator<Person> > >' requested here
    yout << persons;
         ^
/usr/local/include/llvm/Support/YAMLTraits.h:307:8: note: template is declared here
struct MissingTrait;
       ^
1 error generated.
<builtin>: recipe for target 'yaml_project.o' failed
make: *** [yaml_project.o] Error 1

我不认为错误出在我用来编译这个程序的命令中,因为它在编译和 link LLVM 库到我的可执行文件之前对我有用。我认为问题出在代码中,但我无法确定是什么。

提到的头文件llvm/Support/YAMLTraits.h的代码在这里:

https://llvm.org/doxygen/YAMLTraits_8h_source.html

阅读文档,在我看来,支持您的特定 vector<Person> 需要注册宏:

LLVM_YAML_IS_SEQUENCE_VECTOR(Person)
// or
LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(Person)

请参阅实用程序宏:https://llvm.org/docs/YamlIO.html#id22