LoginSignup
0
0

More than 3 years have passed since last update.

gcc で explicit specialization in non-namespace scope エラー

Posted at

クラス内でテンプレート特殊化を定義すると発生します(C++11 想定)

class MyClass {

  template <class T>
  static bool SetValue(const std::vector<T>& v, BaseHandler& handler)  {
    if (!handler.StartArray()) {
      return false;
    }

    for (size_t i = 0; i < v.size(); i++) {
      if (!SetValue(v[i], handler)) {
        return false;
      }
    }

    return handler.EndArray(v.size());
  }

  // Error!
  template<>
  bool SetValue(const std::vector<float>& v, BaseHandler &handler) {
    ...
  }
}

clang, MSVC では発生しないですが, 仕様としては gcc のほうが正しいようです.

解決方法

クラスの外(e.g. .cpp)で特殊化のコードを記述すれば解決します.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0