GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - niXman/static_if: C++14 static_if() alternative implementation from D language

BranchesTags
Open more actions menu

Latest commit

 

History

20 Commits
20 Commits

Folders and files

NameName
Last commit message
Last commit date
include/static_if
include/static_if
 
 
.travis.yml
.travis.yml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
main.cpp
main.cpp
 
 
static_if.pro
static_if.pro
 
 

Repository files navigation

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!

About

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

Resources

Readme
MIT license
Activity

Stars

1 star

Watchers

1 watching

Forks

2 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.