c++ - 在 RAD Studio 2010 中编译 yaml-cpp 时出错

标签 c++ templates yaml c++builder yaml-cpp

我无法编译 yaml-cpp在 RAD Studio 2010 中。我在 nodeutil.h 中有错误

template <typename T, typename U>
struct is_same_type {
enum { value = false };
};

template <typename T>
struct is_same_type<T, T> {
enum { value = true };
};

template <typename T, bool check>
struct is_index_type_with_check {
enum { value = false };
};

template <> struct is_index_type_with_check<std::size_t, false> 
    { enum { value = true }; }; // line 24

#define MAKE_INDEX_TYPE(Type) \
template <> struct is_index_type_with_check<Type, is_same_type<Type, std::size_t>::value> { enum { value = true }; }

MAKE_INDEX_TYPE(int);
MAKE_INDEX_TYPE(unsigned); // line 30
MAKE_INDEX_TYPE(short);
MAKE_INDEX_TYPE(unsigned short);
MAKE_INDEX_TYPE(long);
MAKE_INDEX_TYPE(unsigned long);
#undef MAKE_INDEX_TYPE

编译器打印:

[BCC32 Error] nodeutil.h(30): E2238 Multiple declaration for 'is_index_type_with_check<unsigned int,0>'

[BCC32 Error] nodeutil.h(24): E2344 Earlier declaration of 'is_index_type_with_check<unsigned int,0>'

我认为这一切都是正确的——在第 24 行我得到了

is_index_type_with_check<std::size_t, false> ,

在第 30 行我得到了

is_index_type_with_check<unsigned, true> .

两种不同的类型。

但是如果我像下面这样更改第 24 行,RAD Studio 可以编译 yaml-cpp

template <> struct is_index_type_with_check<std::size_t, true> { enum { value = true }; }; // false -> true

为什么?!在第 24 行我得到了

is_index_type_with_check<std::size_t, true>

第 30 行

is_index_type_with_check<unsigned, true>

两个相同的类型。但所有这些都在 RAD Studio 中工作,而在 MS VS 2008 Express 中不起作用。

最佳答案

用 CodeBlocks 测试你的代码,问题正好相反。这意味着,我的代码编译为

template <> struct is_index_type_with_check<std::size_t, false>

失败了

template <> struct is_index_type_with_check<std::size_t, true>

第 24 行。

问题似乎是,编译器认为哪些类型相同,哪些不同。这个问题分布在编译过程的不同阶段。再看看你的编译器错误。 is_index_type_with_check 的模板参数两者相同 std::size_tunsigned .这意味着,您的编译器认为 std::size_tunsigned是模板参数推导的不同类型(is_same_type::value == false),但后来发现类型推导std::size_tunsigned属于同一类型unsigend int并报错。

总而言之,您没有正确阅读编译器错误 - MAKE_INDEX_TYPE(unsigned);创建了一个 is_index_type_with_check<unsigned int, false> .该类型与您的 template <> struct is_index_type_with_check<std::size_t, false> 冲突编译器提示。

关于c++ - 在 RAD Studio 2010 中编译 yaml-cpp 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2386552/

相关文章:

c++ - C++中牛顿二项式系数的问题

c++ - ZeroMQ的REQ-REP模式如何获取请求者的公网IP?

c++ - 在 Carbon 中创建 JPEG 时的 JPEG 质量

c++ - 仅适用于智能指针的模板

json - openAPI 无法解析对外部文件的引用。组件名称包含无效字符

php - 基于浏览器的 yaml 编辑器,最好使用 PHP?

c++ - 在 C++ 中,如何修改运算符以便在同一条语句中连续调用它两次?

c++ - 模板的模板成员的消歧模板关键字 : when exactly?

c++ - 关于指针值是编译时常量的困惑

python - 我可以在 ruamel.yaml 的 CommentedMap 中插入一行吗?