- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 James McNellis 编写的以下代码来简化声明更多功能的 C++2011 枚举:
#pragma once
#include <stdexcept>
#include <boost/preprocessor.hpp>
#include <vector>
// Internal helper to provide partial specialization for checked_enum_cast
template <typename Target, typename Source>
struct checked_enum_cast_impl;
// Exception thrown by checked_enum_cast on cast failure
struct invalid_enum_cast : std::out_of_range
{
invalid_enum_cast(const char* s)
: std::out_of_range(s) { }
};
// Checked cast function
template <typename Target, typename Source>
Target checked_enum_cast(Source s)
{
return checked_enum_cast_impl<Target, Source>::do_cast(s);
}
// Internal helper to help declare case labels in the checked cast function
#define X_DEFINE_SAFE_CAST_CASE(r, data, elem) case elem:
// How to do this? Question asked on StackOverflow 11/30/14 -- check back again soon.
#define X_ADD_TO_TORET(r, data, elem) ToRet.push_back(elem)
// Defines an enumeration with a checked cast function.
// name is the name of the enumeration to be defined
// enumerators is the preprocessing sequence of enumerators to be defined.
#define DENUM(name, enumerators) \
enum name \
{ \
BOOST_PP_SEQ_ENUM(enumerators) \
}; \
\
template <typename Source> \
struct checked_enum_cast_impl<name, Source> \
{ \
static name cast(Source s) \
{ \
switch (s) \
{ \
BOOST_PP_SEQ_FOR_EACH(X_DEFINE_SAFE_CAST_CASE, 0, enumerators) \
return static_cast<name>(s); \
default: \
throw invalid_enum_cast(BOOST_PP_STRINGIZE(name)); \
} \
return name(); \
} \
}; \
std::vector<name> MembersOf(name AnyItem) { \
return {BOOST_PP_SEQ_ENUM(enumerators)}; \
};
当我用它声明一个枚举时(例如,语法是 DENUM(SYSTEM_STATES, (Loading)(Running)(Finished));),每当包含声明的文件链接到多个对象——甚至,例如,main.obj 和 someclass.obj。具体错误文本:
1>main.obj : error LNK2005: "class std::vector<enum SYSTEM_STATES,class std::allocator<enum SYSTEM_STATES> > __cdecl MembersOf(enum SYSTEM_STATES)" (?MembersOf@@YA?AV?$vector@W4SYSTEM_STATES@@V?$allocator@W4SYSTEM_STATES@@@std@@@std@@W4SYSTEM_STATES@@@Z) already defined in Controller.obj
1>UserInputProcessor.obj : error LNK2005: "class std::vector<enum SYSTEM_STATES,class std::allocator<enum SYSTEM_STATES> > __cdecl MembersOf(enum SYSTEM_STATES)" (?MembersOf@@YA?AV?$vector@W4SYSTEM_STATES@@V?$allocator@W4SYSTEM_STATES@@@std@@@std@@W4SYSTEM_STATES@@@Z) already defined in Controller.obj
1>C:\Users\UserName\Documents\MembersOf investigation\Debug\MembersOf investigation.exe : fatal error LNK1169: one or more multiply defined symbols found
我知道几个月前它工作正常,在我的系统上安装 SFML 之前,但我不认为这是原因;我在甚至没有链接到 SFML 的测试平台项目中重现了这个问题。
在使用上述枚举的 header 中声明的其他内容,以及 boost 的其他用途,都可以正常工作。
最佳答案
您应该标记 MemberOf
inline
。您的另一个选择是将其标记为 static
(如@simon 的评论中所述)。这将链接,但它可能会在使用该函数的每个编译单元中创建 MemberOf
的拷贝。声明 MemberOf
inline
而不是 应该 强制链接器将发出的函数合并为一个。
我之所以说应该,是因为我相信这种行为是实现定义的。在实践中,我相信以上内容适用于所有主要实现,包括 MSVC。
关于c++ - Visual Studio 2013 : linker errors with custom enumerations?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31600849/
挖掘这个,这是一个很酷的 Enumerator(惰性序列)从 1 到(Ruby 可以表示的最大 Float): 1.9.3-p327 :014 > e = (1..Float::INFINITY).e
这个问题在这里已经有了答案: Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequenc
我不确定这是否特定于 ML.NET,但它确实发生在 ML.NET 的上下文中。 我正在使用 ML.NET 对一些图像进行分类。我意识到,无论我是否对结果 IEnumerable 调用 .ToArray
在Collections Framework中,我们有Iterator和ListIterator接口(interface),它们用于迭代数据结构。它们提供了完整的迭代功能。但我想知道为什么 JDK 在
我正在使用 playframework 的异步 I/O 库,它使用 Iteratees 和 Enumerators。我现在有一个 Iterator[T] 作为数据接收器(为简单起见,说它是一个 Ite
更新: private final java.util.Properties tilesPropertyMap = new Properties(); private class Delegati
所以,我有一个像这样的字符串 \begin{enumerate} \item My first item \item My second item \end{enumerate} 并且需要使用正则表达
我有一个数组a = [1, 2, 3, 4, 5] 如果我在数组中有一个元素,我可以通过a[0].next 有没有一种方法可以用来找到前一个元素a[1].previous 或 a[1].before
是否Enumerable#group_by保留每个值内的原始顺序?当我得到这个时: [1, 2, 3, 4, 5].group_by{|i| i % 2} # => {1=>[1, 3, 5], 0=
我已经基于现有的NativeHashMap.Enumerator和UnSafeHashMap.Enumerator结构编写了枚举数,它们以猝发方式工作,但在尝试为嵌套的非托管结构组合枚举数时遇到了麻烦
在 C# 中使用迭代器创建 Enumerable 或 Enumerator 有什么区别?我知道迭代器用于创建实现 Enumerable 或 Enumerator 的私有(private)类... 哪种
自 C++11 过渡以来,GCC 输出警告“条件表达式中的枚举和非枚举类型”。我想了解此警告背后的原因。比较枚举常量有什么危险? 很明显我们可以通过以下方式摆脱这个警告 -Wno-enum-compa
我在 LINQ 查询的性能方面遇到问题,因此我创建了一个简化的小示例来演示下面的问题。该代码采用一个随机的小整数列表,并返回分成几个较小列表的列表,每个列表总计 10 个或更少。 问题是(正如我所写的
我一直对 Enumerable#all? 和 Enumerable#each 的用例感到困惑。例如 ['.txt', '-hello.txt'].all? do |suffix| pu
Enumerable documentation没有明确说明 each 是 each_entry 的别名,但是 each_entry 的描述与我对 each 1 [1, 2] nil Foo.new.
在这段代码中,我创建了一个字符串数组“1”到“10000”: array_of_strings = (1..10000).collect {|i| String(i)} Ruby Core API 是
我正在为我的代码寻求一些帮助,如下所示: for file in file_name : if os.path.isfile(file): for line_number, l
刚刚下载了 Xcode 7 Beta,此错误出现在 enumerate 关键字上。 for (index, string) in enumerate(mySwiftStringArray) { } 谁
刚刚下载了 Xcode 7 Beta,这个错误出现在 enumerate 关键字上。 for (index, string) in enumerate(mySwiftStringArray) { }
为了清楚起见,假设我们有学生和类(class),这是多对多的关系。 我有一个字典,其中键是学生 ID,而 Enumerable 是一个类的集合(假设我们只有 id ),我想将其恢复为 classId、
我是一名优秀的程序员,十分优秀!