Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8585615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:06:35+00:00 2026-06-11T22:06:35+00:00

Consider the following example : template<int X> class MyClass { public: MyClass(int x) {_ncx

  • 0

Consider the following example :

template<int X> class MyClass
{
    public:
        MyClass(int x) {_ncx = x;}
        void test() 
        {
            for (unsigned int i = 0; i < 1000000; ++i) {
                if ((X < 0) ? (_cx > 5) : (_ncx > 5)) {
                    /* SOMETHING */
                } else {
                    /* SOMETHING */
                }
            }
        }
    protected:
        static const int _cx = (X < 0) ? (-X) : (X);
        int _ncx;
};

My question is : will MyClass<-6>::test() and MyClass<6>::test() have a different speed ?

I hope so because in case of a negative template parameter, the if in test function can be evaluated at compile-time, but I’m not sure what is the behaviour of a compiler if there is a compile-time thing and a non-compile-time thing in a ternary operator (which is the case here).

Note : it’s a pure “theoretical” question. If there is a non-null probability of “yes”, I will implement some class for my code with such compile-time template parameters, and if not, I will only provide runtime versions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:06:36+00:00Added an answer on June 11, 2026 at 10:06 pm

    For my compiler ( clang++ v2.9 on OS X ) compiling this similar but not identical code:

    void foo();
    void bar();
    
    template<int N>
    void do_something( int arg ) {
      if ( N<0 && arg<0 ) { foo(); }
      else { bar(); }
    }
    
    // Some functions to instantiate the templates.
    void one_fn(int arg) {
      do_something<1>(arg);
    }
    
    void neg_one_fn(int arg) {
      do_something<-1>(arg);
    }
    

    This generates the following assembly with clang++ -S -O3.

    one_fn = do_something<1>

    The first functions assembly clearly only has the call to bar.

        .globl  __Z6one_fni
        .align  4, 0x90
    __Z6one_fni:                            ## @_Z6one_fni
    Leh_func_begin0:
        pushl   %ebp
        movl    %esp, %ebp
        popl    %ebp
        jmp __Z3barv                ## TAILCALL
    Leh_func_end0:
    

    neg_one_fn = do_something<-1>

    The second function has been reduced to a simple if to call either bar or foo.

        .globl  __Z10neg_one_fni
        .align  4, 0x90
    __Z10neg_one_fni:                       ## @_Z10neg_one_fni
    Leh_func_begin1:
        pushl   %ebp
        movl    %esp, %ebp
        cmpl    $0, 8(%ebp)
        jns LBB1_2                  ## %if.else.i
        popl    %ebp
        jmp __Z3foov                ## TAILCALL
    LBB1_2:                                 ## %if.else.i
        popl    %ebp
        jmp __Z3barv                ## TAILCALL
    Leh_func_end1:
    

    Summary

    So you can see that the compiler inlined the template, then optimised away the branch when it could. So the kind of transformation you are hoping for does occur in current compilers. I got similar results (but less clear assembly) from an old g++ 4.0.1 compiler too.

    Addendum:

    I decided this example wasn’t quite similar enough to your initial case (as it didnt’ involve the ternary operator) so I changed it to this: (Getting the same kind of results)

    template<int X>
    void do_something_else( int _ncx ) {
      static const int _cx = (X<0) ? (-X) : (X);
      if ( (X < 0) ? (_cx > 5) : (_ncx > 5)) {
        foo();
      } else {
        bar();
      }
    }
    
    void a(int arg) {
      do_something_else<1>(arg);
    }
    
    void b(int arg) {
      do_something_else<-1>(arg);
    }
    

    This generates the assembly

    a() = do_something_else<1>

    This still contains the branch.

    __Z1ai:                                 ## @_Z1ai
    Leh_func_begin2:
        pushl   %ebp
        movl    %esp, %ebp
        cmpl    $6, 8(%ebp)
        jl  LBB2_2                  ## %if.then.i
        popl    %ebp
        jmp __Z3foov                ## TAILCALL
    LBB2_2:                                 ## %if.else.i
        popl    %ebp
        jmp __Z3barv                ## TAILCALL
    Leh_func_end2:
    

    b() = do_something_else<-1>

    Branch is optimised away.

    __Z1bi:                                 ## @_Z1bi
    Leh_func_begin3:
        pushl   %ebp
        movl    %esp, %ebp
        popl    %ebp
        jmp __Z3barv                ## TAILCALL
    Leh_func_end3:
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following example template<class Type = void> class MyClass { public: double getValue()
Consider the following example: template <typename T> class A { public: void f() {
Consider the following example: class MyContainer { std::vector<void *> v; public: void Put(void *x)
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider the following small example: template<typename T> void foo(T a) { ... } struct
Consider the following example code: class Foo { }; class Bar : public Foo
Consider following example. #include <iostream> #include <algorithm> #include <vector> #include <boost/bind.hpp> void func(int e,
Consider following example. #include <iostream> #include <boost/optional.hpp> template < typename A > int boo(
Consider the following contrived example of a templated array definition: template <typename t, unsigned
Consider the following code : template<unsigned int... TSIZE> struct Base {}; template<unsigned int TORDER,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.