简体   繁体   中英

Can static template functor be inlined by compiler?

I have read following questions:

  1. Template functors vs functions
  2. C++ Functors - and their uses
  3. C++ function template partial specialization?

And I understand what C++ functors are good for. But I can't deduce what will happen if will do the following:

template <typename T, unsigned int state>
class Foo {
public:
    static Foo_func() { /* Do something */ };
}

// Partial specialization:
// -- state == 1
template <typename T>
class Foo <T, 1> {
public:
    static Foo_func() { /* Do something */ };
}

template <typename F>
void call_func(F func) {

    // Do something... //

    func();

    // Do something... //
}

int main() {
    Foo <double, /*state*/ 1> obj;

    // Case 1:
    call_func(obj.Foo_func);

    // Case 2:
    call_func(Foo<double, /*state*/ 1>::Foo_func);
}

In which scenario compiler will be able to inline Foo_func() ?

But I can't deduce what will happen if will do the following

It's not possible to deduce what will happen, you just have to try it and see.

Inlining is an optimization, not a language feature. When and whether it happens depends on your compiler, its version, how you configure it, and possibly lots of other context around the (possibly in-lined) call site.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM