title: Visual Studio 2013: linker errors with custom enumerations?
body: <p>I'm using the following code, written by James McNellis, to simplify declaring more-functional C++2011 enums:</p>
  
  <pre><code>#pragma once
  #include &lt;stdexcept&gt;
  #include &lt;boost/preprocessor.hpp&gt;
  #include &lt;vector&gt;
  
  // Internal helper to provide partial specialization for checked_enum_cast
  template &lt;typename Target, typename Source&gt;
  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 &lt;typename Target, typename Source&gt;
  Target checked_enum_cast(Source s)
  {
      return checked_enum_cast_impl&lt;Target, Source&gt;::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 &lt;typename Source&gt;                                             \
  struct checked_enum_cast_impl&lt;name, Source&gt;                            \
  {                                                                      \
      static name cast(Source s)                                      \
      {                                                                  \
          switch (s)                                                     \
          {                                                              \
          BOOST_PP_SEQ_FOR_EACH(X_DEFINE_SAFE_CAST_CASE, 0, enumerators) \
              return static_cast&lt;name&gt;(s);                               \
          default:                                                       \
              throw invalid_enum_cast(BOOST_PP_STRINGIZE(name));         \
          }                                                              \
          return name();                                                 \
      }                                                                  \
  };                                                                  \
  std::vector&lt;name&gt; MembersOf(name AnyItem) {     \
      return {BOOST_PP_SEQ_ENUM(enumerators)}; \
  }; 
  </code></pre>
  
  <p>When I declare an enumeration with this (the syntax is DENUM(SYSTEM_STATES, (Loading)(Running)(Finished));, for example), MembersOf() generates a linker error whenever the file containing the declaration is linked to multiple objects -- even, for example, main.obj and someclass.obj. Specific error texts:</p>
  
  <pre><code>1&gt;main.obj : error LNK2005: "class std::vector&lt;enum SYSTEM_STATES,class std::allocator&lt;enum SYSTEM_STATES&gt; &gt; __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&gt;UserInputProcessor.obj : error LNK2005: "class std::vector&lt;enum SYSTEM_STATES,class std::allocator&lt;enum SYSTEM_STATES&gt; &gt; __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&gt;C:\Users\UserName\Documents\MembersOf investigation\Debug\MembersOf investigation.exe : fatal error LNK1169: one or more multiply defined symbols found
  </code></pre>
  
  <p>I know that this worked properly a couple of months ago, before I installed SFML on my system, but I don't think that it's the cause; I reproduced this problem in a testbed project where I'm not even linking to SFML.</p>
  
  <p>Other things declared in a header that uses the above enums, and other uses of boost, work properly.</p>