2

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?

8
  • 1
    "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" I don't understand what you mean by this. Can you give some actual examples of what the "working" version "without a class" means, what you mean by "put the function to the class", and what your errors actually are? Sep 24, 2021 at 18:16
  • @NathanPierson If I declare fun in the CPP file as a function it works. If I declare fun in class in the CPP file it doesn't work. I've updated my question and include an example and errors. Sep 24, 2021 at 18:28
  • This still doesn't show an example of the code that doesn't work. I don't think many people are going to be able to help you debug code that you aren't showing. Your "I need something like this" version works fine for me. Sep 24, 2021 at 18:33
  • @NathanPierson I don't show my code 'cos there is nothing to show. I want exactly what you wrote on the web page from your link. But it doesn't work for me. I even copy/paste your code but it doesn't work. I tried g++ and g++-10. Main error: «double A::fun()» cannot be overloaded with «bool A::fun()» Sep 24, 2021 at 18:43
  • Apparently clang is fine with this and g++ is not. Weird. I don't know which one is correct. However, what you can do is provide non-inline definitions for your member function specializations. Sep 24, 2021 at 18:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.