Is there any way I can emulate C++ value template parameters in C#?
template<bool flag>
void Method()
{
// Do some work
if constexpr(flag)
{
// Do something specific
}
// Do some more work
}
So that it would generate two versions of a method which can be called like this:
Method<false>();
Method<true>();
This is for performance reasons, so it is better to not make additional calls inside the Method
. The Method
is performance critical part and it is called billions of times so it is important to squeeze every CPU cycle from it.
On the other hand it has a rather complicated logic, so I would prefer to not have two copies of it.
I think I could do something with generics, but it wouldn't be the best option for performance. So now the only way I can think of is to create some kind of template code generator for it. But maybe there are other options? Maybe it is possible to compile two versions of the method using Roslyn, so that it would create two optimized versions of it with specific arguments?
void Method(bool flag)
{
// Do some work
if (flag)
{
// Do something specific
}
// Do some more work
}
So that I can compile it to:
void Method_false(false)
{
// Do some work
// Do some more work
}
and to:
void Method_true(true)
{
// Do some work
// Do something specific
// Do some more work
}
Is it at all possible?