Skip to content

Instantly share code, notes, and snippets.

@t-mat
Created August 19, 2014 09:14
Show Gist options
  • Save t-mat/f3caa7a44270a8767158 to your computer and use it in GitHub Desktop.
Save t-mat/f3caa7a44270a8767158 to your computer and use it in GitHub Desktop.
Visual Studio 14 CTP 3 : C++11 constexpr testing
// (1) In VS14 CTP3, implementation of constexpr is still partial.
// http://blogs.msdn.com/b/vcblog/archive/2014/06/11/c-11-14-feature-tables-for-visual-studio-14-ctp1.aspx
//
#include <stdio.h>
#include <stdint.h>
// (2) Straightforward FOURCC
constexpr uint32_t fourcc(const char* p) {
return (static_cast<uint32_t>(p[0]) << (8*0))
| (static_cast<uint32_t>(p[1]) << (8*1))
| (static_cast<uint32_t>(p[2]) << (8*2))
| (static_cast<uint32_t>(p[3]) << (8*3));
}
void test_fourcc() {
const uint32_t c = ('F' << 0) | ('O' << 8) | ('U' << 16) | ('R' << 24);
switch(c) {
default: printf("c = ?\n"); break;
case fourcc("FOUR"): printf("c = four\n"); break;
case fourcc("FIVE"): printf("c = five\n"); break;
}
}
// (3) VS14 CTP3 could not compile Clement JACOB's CRC32.
// http://stackoverflow.com/a/9842857/2132223
// (4) Jerry Coffin's constexpr hash function.
// http://stackoverflow.com/a/2112111/2132223
uint32_t constexpr const_hash0(const char* p, int i) {
return p[i] == 0
? 5381
: static_cast<uint32_t>(p[i]) + 33 * const_hash0(p, i+1)
;
}
uint32_t constexpr const_hash(const char* p) {
return const_hash0(p, 0);
}
void test_hash() {
enum : uint32_t {
h = const_hash("ABCD"),
};
printf("h = %08x\n", h);
}
//
int main() {
test_hash();
test_fourcc();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment