Top/article/Specialize-template-member-methods-outside-the-class
English | Japanese
English | Japanese

Menu

  • Top
  • Akihiko Yamaguchi 山口 明彦
  • Project プロジェクト
  • Text テキスト
  • Recent articles 最近の記事
  • Articles 記事一覧 (a to z)
  • Search 検索

Tags タグ †

  • [c++][bash][python][latex][php]
  • [linux][windows][mac][android]
  • [math][algorithm][idea][trick]
  • [liboctave][opencv][git][ros]
  • [setting][bug][general]
↑

Recent articles 最近の記事 †

2019-07-02
  • article/Display-Unix-Time
  • article/Synchronize-Linux-Time-to-Remote
2018-09-27
  • article/python-multimode-singleton
2018-09-02
  • article/rosinstall-git-default-remote
2017-07-28
  • article/SubMenu
2017-03-05
  • article/Import-a-different-version-of-OpenCV-in-Python
2015-08-17
  • article/DRC-finals-2015
2015-01-05
  • article/Upgrade-Android-to-Lollipop
2015-01-01
  • article/Kernel-panic-of-Linux-when-using-Xtion
  • article/Do-not-skip-freeing-data-when-using-tri-mesh-in-ODE
Access: 2/1903 - Admin
These search terms have been highlighted:[c++]

Specialize template member methods outside the class

テンプレートメンバ関数の特殊化はクラス外で

[c++]
2012-03-04

クラスのメンバ関数をテンプレート関数として作成し,さらにそれを特殊化する場合,特殊化は,特殊化宣言も含めてクラス外で行わなければならないらしい.

NG例 †

class TTest
{
public:
  template<typename T> T one(void) const {return 1;}
  template<> double one<double>(void) const {return 1.0;} // 特殊化
};

コンパイルすると,

15: error: explicit specialization in non-namespace scope ‘class TTest’
15: error: template-id ‘one<double>’ in declaration of primary template

というエラーが出る(g++-4.4.4 の場合).

↑

OK例 †

class TTest
{
public:
  template<typename T> T one(void) const {return 1;}
};
template<> double TTest::one<double>(void) const {return 1.0;} // 特殊化

こう書けばOK.

↑

参考 †

  • C++テンプレート



Last-modified:2015-01-01 (Thu) 09:30:26 (3834d)
Site admin: Akihiko Yamaguchi.
Written by: Akihiko Yamaguchi.
System: PukiWiki 1.5.0. PHP 5.2.17. HTML conversion time: 0.008 sec.