Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Can Anybody explain what is basic syntax of given specialize template?

Can Anybody explain what is basic syntax of given specialize template?

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 3 Posters 174 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote 28 days ago last edited by
    #1

    I want to know why after class name <R(Args...)> written ?

    template <typename T>
    class Logger3;
    
    template <typename R, typename... Args>
    class Logger3<R(Args...)>
    {
      std::function<R(Args...)> func_;
      std::string name_;
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote 28 days ago last edited by
      #2

      Hi,

      This means that this template class is specialized for a function that returns the R type and accepts Args... arguments. For example:

      Logger3<int (const char *)>
      

      For a more detailed explanation, see this Stack Overflow post.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Q 1 Reply Last reply 27 days ago
      4
      • S SGaist
        28 days ago

        Hi,

        This means that this template class is specialized for a function that returns the R type and accepts Args... arguments. For example:

        Logger3<int (const char *)>
        

        For a more detailed explanation, see this Stack Overflow post.

        Q Offline
        Q Offline
        Qt embedded developer
        wrote 27 days ago last edited by Qt embedded developer
        #3

        @SGaist Thank you.
        I need your help for further queries:
        Can you provide me basic syntax for it because without basic syntax its difficult to digest because i am completely unaware theoretical concept. where someone say you need to pass just

        function return type (Arguments of function ) after class name

        ex : template <typename R, typename... Args>
        class Logger3 <R(Args...)>

        if i am not knowing basic syntax of it then how i can in future think of this type of logic?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SimonSchroeder
          wrote 27 days ago last edited by
          #4

          In general you define a simple template class like this:

          template<typename T>
          class A
          {
          };
          

          This can then be used with

          A<int> myA;
          

          Sometimes you want to have a different implementation for a specific type which we call a template specialization:

          template<>
          class A<int>
          {
          };
          

          Just as with function overloading where the compiler picks the function with the best fit for the parameter types, the compiler will pick the most specific specialization.

          Sometimes you don't want a specific type but some specific sort of type, e.g. a pointer:

          template<typename T>
          class A<T*>
          {
          };
          

          Now, if you write A<int*> pInt; or A<double*> pDouble; it will match the specialization for pointers. In the same way class Logger3<R(Args...)> will match any function as template argument. One of the "modern C++" features is Args... which means any number of types (0 or more). (The ... introduces a list of types.) So, R(Args...) can match void() or void(int) or int(double,int) etc. Because we are just interested in the function signature this looks like a function declaration, but without any function name between the return type and the argument list.

          1 Reply Last reply
          5
          • Q Qt embedded developer has marked this topic as solved 26 days ago

          4/4

          16 Apr 2025, 06:40

          • Login

          • Login or register to search.
          4 out of 4
          • First post
            4/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved