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. Type specific code compilation within function
Forum Updated to NodeBB v4.3 + New Features

Type specific code compilation within function

Scheduled Pinned Locked Moved Unsolved C++ Gurus
3 Posts 3 Posters 381 Views 3 Watching
  • 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
    Q139
    wrote on last edited by Q139
    #1

    Hi,
    If constructing function that has variadic parameter list and some input parameters are not compatable with function calls within the variadic function.

    What methods can be used within same function to instruct compiler to compile specific code for each input variable type?

    I understand template functions could be used but it means lot more code.

    Perhaps lambdas support variable specific execution or something similar.

    Currently variables are filtered using typeid(xx).name() but not all code is compatable with all variable types.

    kshegunovK 1 Reply Last reply
    0
    • Q Q139

      Hi,
      If constructing function that has variadic parameter list and some input parameters are not compatable with function calls within the variadic function.

      What methods can be used within same function to instruct compiler to compile specific code for each input variable type?

      I understand template functions could be used but it means lot more code.

      Perhaps lambdas support variable specific execution or something similar.

      Currently variables are filtered using typeid(xx).name() but not all code is compatable with all variable types.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @Q139 said in Type specific code compilation within function:

      Hi,
      If constructing function that has variadic parameter list and some input parameters are not compatable with function calls within the variadic function.

      What sort of variadic are we talking about? Example code please!

      What methods can be used within same function to instruct compiler to compile specific code for each input variable type?

      SFINAE if you are working with templates, otherwise (i.e. C variadics), none, they're not and will never be type-safe.

      I understand template functions could be used but it means lot more code.

      Could be used. A lot more code is a matter of debate, depending on the code, implementation, etc.

      Perhaps lambdas support variable specific execution or something similar.

      I have no idea what you mean. C++ is statically typed, so every type, no matter how complicated it may be shall be known at compile time.

      Currently variables are filtered using typeid(xx).name() but not all code is compatable with all variable types.

      This is a runtime-evaluated operator. It basically resolves the platform specific decorated type name. However I don't understand what is the relation to the top part of this post. Elaborate please.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Q139 said:

        What methods can be used within same function to instruct compiler to compile specific code for each input variable type?

        Lets say you want to do something for ints and something else for all other types:

        void doSomethingWithInt(int) { qDebug() << "I'm an int"; }
        void doSomethingElse(auto) { qDebug() << "I'm not an int"; }
        

        You can choose the right function at compile time with if constexpr and type traits:

        template <typename T>
        void doStuffToOneItem(T t)
        {
            if constexpr (std::is_same_v<T,int>)
                doSomethingWithInt(t);
            else
                doSomethingElse(t);
        }
        

        and then you can use it in a variadic function with the usual ugly parameter pack shenanigans:

        template <typename ...Args>
        void foo(Args&&... args)
        {
            std::initializer_list<int>{(doStuffToOneItem(std::forward<Args>(args)), 0)...};
        }
        

        and call it:

        foo("hello", 42, 17.3f);
        
        1 Reply Last reply
        2

        • Login

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