Questions tagged [static-if]

12 questions
13
votes
4 answers

static if in plain c++?

Problem in short: How could one implement static if functionality, proposed in c++11, in plain c++ ? History and original problem: Recently I came up with a problem like this. I need a class Sender with an interface like class Sender { void…
Grigor Gevorgyan
  • 6,753
  • 4
  • 35
  • 64
13
votes
3 answers

Are there other languages besides D with static if?

I think D's static if is an interesting language feature. That prompts my question: Are there are other examples of compiled languages in which the compiler has a strong notion of the code and there are languages facilities to access them? For…
Grumdrig
  • 16,588
  • 14
  • 58
  • 69
8
votes
3 answers

Faking Static If in C++

I am testing combinations of various optimizations and for these I need a static-if as described in http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Static-If-I-Had-a-Hammer to enable and disable specific optimizations. if(const-expr)…
B.S.
  • 1,435
  • 2
  • 12
  • 18
5
votes
2 answers

Is D's "static if" declarative or procedural?

Consider the following code: static if (!is(MyStruct)) { struct MyStruct { } } static if (is(MyStruct)) { static assert(0); } My original understanding has been that the order of declarations (in global scope) does not matter in…
user541686
  • 205,094
  • 128
  • 528
  • 886
5
votes
6 answers

Will static_if deprecate template specialization?

Some usual template specialization like this: template class C { void common() { ... } void f2 = delete; }; template<> class C { void common() { ... } void f1() { ... } }; Could be represented with static_if…
David
  • 27,652
  • 18
  • 89
  • 138
4
votes
4 answers

static_if in C99's preprocessor

Is it possible to implement static_if in C99? #define STATIC_IF(COND, ...) \ if (COND) MACRO1(__VA_ARGS__); \ else MACRO2(__VA_ARGS__); How can I properly implement STATIC_IF(…) in here? Depending on COND the arguments either should be…
Kijewski
  • 25,517
  • 12
  • 101
  • 143
3
votes
1 answer

Avoid function call on compile time false condition

I want to be able avoid calling a function when a condition is false when this is known at compile time. Now I use something like this: template void fun(params) { //do nothing } template<> void fun(params) { //do…
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
2
votes
1 answer

"static-if" better with classes or function templates?

I want to define interface for serializing variables, where depending on a template argument, serialization code (true) or nothing (false) is performed. The serialization function is itself templated on archive and variable types. Is it better to…
eudoxos
  • 18,545
  • 10
  • 61
  • 110
1
vote
4 answers

Is there a C++ programming technique which is an approximate equivalent of a runtime #ifdef?

For example I have a function call in some code I want to be able to enable/disable as I like. Normally I could just use an if, but then it would check each time if the function can be ran, which I don't want. I simply want the code to never do that…
jokoon
  • 6,207
  • 11
  • 48
  • 85
0
votes
1 answer

If statements on compile-time const value

I want to have code included in a function based on a compile time constant value, but static_if is not a construct in C++. So I can write functions like this class TA { public: template void func() { if(flag) …
Generic Name
  • 1,083
  • 1
  • 12
  • 19
0
votes
0 answers

Ensure whether specific code is emitted or not

How can I ensure whether some specific code is not emitted? Say, I tried to write something like a static switch (or static if) and I wish to detect whether second lambda operator ()'s body is emitted: #include namespace…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
-1
votes
1 answer

C++11 Class template method specialization and variadic templates

I am trying to construct the following kind of templated method using GCC and C++11: class Foo { private: //Device handle cudnnHandle_t handle_; //Batch sizes std::vector batch_; public: Foo(cudnnHandle_t handle,…
Fiorentino
  • 101
  • 6