Giter VIP home page Giter VIP logo

static_if's Introduction

Build Status

static_if

C++14 static_if() alternative implementation from D language

  • // Emulation of "static if" feature from D language within function body, alpha version.
  • // Original idea by Maxim Yanchenko (refer https://rsdn.ru/forum/cpp/5816278.flat.1)
  • // Some improvements by Evgeny Panasyuk
  • // Some improvements by niXman

Features:

  • allows to use it as expression: auto r = STATIC_IF(...) { return 3; } STATIC_ELSE() { return 4; };
  • allows to have different result type of an expression: auto r = STATIC_IF(...) { return 3; } STATIC_ELSE() { return 4.0; };
  • allows to have different branches depending on constexpr-condition: auto r = STATIC_IF(is_same(T, std::string), v) { return v + "3"; } STATIC_ELSE(v) { return v + 4.0; };
  • allows to have constexpr result when C++17 is used
  • zero run-time overhead

Overhead:

For the following code sample:

template<typename T>
__attribute__ ((noinline))
T foo(const T &v) {
    return STATIC_IF(is_same(T, int), v) {
        return v+3;
    } STATIC_ELSE(v) {
        return v+4;
    };
}

int main(int argc, char **argv) {
    int a = foo(argc);
    int b = *foo(argv[argc]);
    return a + b;
}

the following ASM will be generated:

char* foo<char*>(char* const&):
        lea     rax, [rdi+4]
        ret
int foo<int>(int const&):
        lea     eax, [rdi+3]
        ret
main:
        call    foo<int>(int const&)
        movsx   rdi, edi
        mov     rdi, QWORD PTR [rsi+rdi*8]
        mov     edx, eax
        call    foo<char*>(char* const&)
        movsx   eax, BYTE PTR [rax]
        add     eax, edx
        ret

As you can see, there is nothing superfluous here!

static_if's People

Contributors

nixman avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

antmd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.