c++ - 使用 clang 和 g++ 编译此 "simple"程序时出现链接错误

标签 c++ c++11 g++ constexpr clang++

我正在开发一个项目,似乎 clang 无法生成有效的字节码(由于链接器无法链接,因此在链接时找不到模板类中的某些 static constexpr ) 我可以使用类中的静态 getter 来修复它,但这会导致一些非常丑陋/重载的代码。

这是一个“最小”的代码示例,它使错误(这是一个错误吗?)出现。 不幸的是,在这里,g++ 将产生相同的链接错误。

我问这是否是编译器错误,或者我只是做错了什么,以及是否有解决方案可以避免此错误。 (如果我做错了什么,为什么相同的构造在我更大的项目中起作用??)

注意:更大的项目在 github 上名为 yaggler,但仍处于其生命的早期阶段

#include <type_traits>
#include <iostream>

// exemple vector classes
struct vector2
{
  constexpr vector2(unsigned int _x = 0, unsigned int _y = 0) : x(_x), y(_y) {} // for clang

  unsigned int x;
  unsigned int y;
};
struct vector3 : public vector2 // uh ;)
{
  constexpr vector3(unsigned int _x = 0, unsigned int _y = 0, unsigned int _z = 0) : vector2(_x, _y), z(_z) {} // for clang
  unsigned int z;
};

// simple templated generic vector type
// we could make a more generic one, but this would require something like a tuple.
template<unsigned int... Vals>
struct vector
{
    static_assert(!(sizeof...(Vals) + 1), "[...]");
};
template<unsigned int X>
struct vector<X>
{
  using vec_type = unsigned int;
  static constexpr unsigned int value = X;
};
template<unsigned int X, unsigned int Y>
struct vector<X, Y>
{
  using vec_type = vector2;
  static constexpr vector2 value = vector2(X, Y);
};
template<unsigned int X, unsigned int Y, unsigned int Z>
struct vector<X, Y, Z>
{
  using vec_type = vector3;
  static constexpr vector3 value = vector3(X, Y, Z);
};

// a simple wrapper
template<typename V>
struct some_wrapper
{
  static constexpr typename V::vec_type value = V::value;
};

// a dummy function that print something to stdout.
void do_something(int32_t id, const vector3 &value)
{
  std::cout << id << " " << value.z << std::endl;
}
void do_something(int32_t id, const vector2 &value)
{
  std::cout << id << " " << value.y << std::endl;
}
void do_something(int32_t id, int value)
{
  std::cout << id << " " << value << std::endl;
}

// the class used to create the error
template< typename... Args>
class exemple
{
  private:
    // an initialisation that recurse over the Args... template arguments
    template<typename Current, typename... Others>
    void __rec_init() const
    {
      do_something(0, Current::value);
      __rec_init<Others...>();
    }

    // end of recursion
    template<size_t = 0>
    void __rec_init() const {}

    // launch the recursion
    void tpl_init() const
    {
      __rec_init<Args...>();
    }

  public:
    exemple()
    {
      tpl_init();
    }
};

int main()
{
  // and here, we get a linker error.
  exemple<some_wrapper<vector<4, 4, 5>>, some_wrapper<vector<4, 1>>, some_wrapper<vector<9>>>();
}

编辑:仅提及 gcc 和 clang 版本:gcc 4.7.3/4.8.2 和 clang 3.2/3.3

最佳答案

vector的特化2 个和 3 个模板参数的类模板有 static constexpr数据成员value没有命名空间范围定义的文字类型(分别为 vector2vector3 )。

您需要它们,因为您使用 value当它绑定(bind)到传递给do_something时的引用参数时功能。

§9.4.2/3 [class.static.mfct]

If a non-volatile const static data member is of integral or enumeration type, its declaration in the class definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignment- expression is a constant expression (5.19). A static data member of literal type can be declared in the class definition with the constexpr specifier; if so, its declaration shall specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression. [Note: In both these cases, the member may appear in constant expressions. —end note ] The member shall still be defined in a namespace scope if it is odr-used (3.2) in the program and the namespace scope definition shall not contain an initializer.

编辑:纠正我自己,实际上是 some_wrapper<T>::value需要这个定义(尽管如此,出于上述原因)。所以你需要的是在some_wrapper定义之后的命名空间范围内的这个。 :

template<typename V>
constexpr typename V::vec_type some_wrapper<V>::value;

之后,您的代码 compiles and runs .

关于c++ - 使用 clang 和 g++ 编译此 "simple"程序时出现链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21344031/

相关文章:

c++ - 在 OpenGL 中删除部分绘图

c++ - 启用默认初始化列表构造函数

linux - 在 CentOS5 Box 上运行 Ubuntu 12.04 编译的 C++ 程序

c++ - 使用 gcc 在 C++ 中嵌入常量循环条件优化

c++ - 如何在 QML 场景上绘制 3D 线?

c++ - 从任务栏隐藏 "Close Window"选项

c++ - "Materializing"用于 C++ 类型推断的已知类型的对象

c++ - map 的键可以与部分值共享吗?

c++ - 返回对齐结构后出现段错误

c++ - 我对内存泄漏有什么不了解?