Function with template specialization in class without wrapping by class

Viewed 179

I have a class and I wanna specialize a function in that class. I need something like this:

class A
{
public:
    template <typename T>
    T fun()
    {
        throw new Exception("Unsupported template param");
    }

    template <>
    bool fun<bool>()
    {
        return true;
    }

    template <>
    float fun<float>()
    {
        return 5.6f;
    }

    template <>
    double fun<double>()
    {
        return 5.684;
    }
};

It works if I use the function without a class but if I put the function to the class I will get a lot of errors.

Example:

 template <>
    bool fun<bool>()
    {
        return true;
    }

    template <>
    float fun<float>()
    {
        return 5.6f;
    }

    template <>
    double fun<double>()
    {
        return 5.684;
    }

It works. But when I declare fun in some class it doesn't work. Errors: (Sorry for Russian language but my OS is translated to russian)

/home/aleksey/Документы/Проекты/tests/color-c/source/app.cpp:12:15: ошибка: явная специализация в не-namespace области «class A»
   12 |     template <>
      |               ^
/home/aleksey/Документы/Проекты/tests/color-c/source/app.cpp:13:10: ошибка: template-id «fun<bool>» in declaration of primary template
   13 |     bool fun<bool>()
      |          ^~~~~~~~~
/home/aleksey/Документы/Проекты/tests/color-c/source/app.cpp:18:15: ошибка: явная специализация в не-namespace области «class A»
   18 |     template <>
      |               ^
/home/aleksey/Документы/Проекты/tests/color-c/source/app.cpp:19:11: ошибка: template-id «fun<float>» in declaration of primary template
   19 |     float fun<float>()
      |           ^~~~~~~~~~
/home/aleksey/Документы/Проекты/tests/color-c/source/app.cpp:19:11: ошибка: «float A::fun()» cannot be overloaded with «bool A::fun()»
/home/aleksey/Документы/Проекты/tests/color-c/source/app.cpp:13:10: замечание: предыдущая декларация «bool A::fun()»
   13 |     bool fun<bool>()
      |          ^~~~~~~~~
/home/aleksey/Документы/Проекты/tests/color-c/source/app.cpp:24:15: ошибка: явная специализация в не-namespace области «class A»
   24 |     template <>
      |               ^
/home/aleksey/Документы/Проекты/tests/color-c/source/app.cpp:25:12: ошибка: template-id «fun<double>» in declaration of primary template
   25 |     double fun<double>()
      |            ^~~~~~~~~~~
/home/aleksey/Документы/Проекты/tests/color-c/source/app.cpp:25:12: ошибка: «double A::fun()» cannot be overloaded with «bool A::fun()»
/home/aleksey/Документы/Проекты/tests/color-c/source/app.cpp:13:10: замечание: предыдущая декларация «bool A::fun()»
   13 |     bool fun<bool>()
      |          ^~~~~~~~~
/home/aleksey/Документы/Проекты/tests/color-c/source/app.cpp: В функции-члене «T A::fun()»:
/home/aleksey/Документы/Проекты/tests/color-c/source/app.cpp:9:19: ошибка: expected type-specifier before «Exception»
    9 |         throw new Exception("unsupported colormodel");
      |                   ^~~~~~~~~
/home/aleksey/Документы/Проекты/tests/color-c/source/app.cpp: В функции «int main(int, const char**)»:
/home/aleksey/Документы/Проекты/tests/color-c/source/app.cpp:33:18: ошибка: нет декларации «fun» в этой области видимости
   33 |     auto test1 = fun<float>();
      |                  ^~~
/home/aleksey/Документы/Проекты/tests/color-c/source/app.cpp:33:22: ошибка: expected primary-expression before «float»
   33 |     auto test1 = fun<float>();
      |                      ^~~~~
/home/aleksey/Документы/Проекты/tests/color-c/source/app.cpp:34:22: ошибка: expected primary-expression before «double»
   34 |     auto test2 = fun<double>();
      |                      ^~~~~~
/home/aleksey/Документы/Проекты/tests/color-c/source/app.cpp:35:22: ошибка: expected primary-expression before «int»
   35 |     auto test3 = fun<int>();

I googled some similar questions about this purpose but most of the answers suggest only solutions with templates class, not template function in non-templated class. However, I've found this question. But the date of this question is 2014. Since the date of this one c++ get many changes. Maybe is there any way to specialize a function in a class without wrapping the function to separate class?

0 Answers
Related