2

私は取り組んでいますVisual Studio 2015 community edition

次のような単純なクラスがあるとしましょう:
(以下の例は、必要なものがすべて含まれているため、コンパイル可能である必要がありますが、残念ながらエラーが発生します)。

#include <stdexcept>

template <typename T>
class class_foo
{
// members, methods, constructors. not important stuff...

// helper functions:
private:
    class tag_aaa {}; // to resolve few things at compile-time, not run-time.
    class tag_bbb {}; // - || -

    template <typename tag>
    void erase();

    // for some reason this is not interpreted as an error by my compiler:
    template<>
    void erase<tag_aaa>();

    template<>
    void erase<tag_bbb>();
};

// catch-all-do-nothing "version"
// well, catch-all-throw-an-exception because call to this function is an obvious error.
// that should never occur.
template <typename T>
template <typename tag> inline
void class_foo<T>::erase()
{
    throw std::runtime_error("Very weird error...");
}

template <>
template <typename T> inline
void class_foo<T>::erase<class_foo<T>::tag_aaa>()
{
    // do some stuff... 
}

template <>
template <typename T> inline
void class_foo<T>::erase<class_foo<T>::tag_bbb>()
{
    // do some stuff... 
}

int main()
{
    class_foo<double> bar;

    return 0;
}

エラー:

1>D:/develop/workspace/visual_studio/nevada_test_site/source/workspace/nevada_test_site/start.cu(36): error : partial specialization of class "class_foo<T>::erase<class_foo<T>::tag_aaa> [with T=T]" is not allowed

1>D:/develop/workspace/visual_studio/nevada_test_site/source/workspace/nevada_test_site/start.cu(43): error : partial specialization of class "class_foo<T>::erase<class_foo<T>::tag_bbb> [with T=T]" is not allowed

1>D:/develop/workspace/visual_studio/nevada_test_site/source/workspace/nevada_test_site/start.cu(51): warning : variable "bar" was declared but never referenced

私は自分自身を初心者の趣味のプログラマーと考えているので、確かに間違っていますが、 とerase<class_foo<T>::tag_aaa>()の両方erase<class_foo<T>::tag_bbb>()が関数の明示的な特殊化であると信じていtemplate <typename tag> void erase();ます。そのため、それらは許可されています。このエラーは構文の誤りによるものだと思いますが、エラーが見つかりません。

質問:

  • 私がやろうとしていることは許可されていますか?
  • はいの場合、何が間違っていますか?
  • はいの場合、この関数を特殊化するための正しい構文は何ですか ( erase)?
4

1 に答える 1