Bit count : preprocessor magic vs modern C++

Why not use the standard library? #include <bitset> int bits_in(std::uint64_t u) { auto bs = std::bitset<64>(u); return bs.count(); } resulting assembler (Compiled with -O2 -march=native): bits_in(unsigned long): xor eax, eax popcnt rax, rdi ret It is worth mentioning at this point that not all x86 processors have this instruction so (at least with gcc) you … Read more

C++: Can a macro expand “abc” into ‘a’, ‘b’, ‘c’?

I’ve created one today, and tested on GCC4.6.0. #include <iostream> #define E(L,I) \ (I < sizeof(L)) ? L[I] : 0 #define STR(X, L) \ typename Expand<X, \ cstring<E(L,0),E(L,1),E(L,2),E(L,3),E(L,4), E(L,5), \ E(L,6),E(L,7),E(L,8),E(L,9),E(L,10), E(L,11), \ E(L,12),E(L,13),E(L,14),E(L,15),E(L,16), E(L,17)> \ cstring<>, sizeof L-1>::type #define CSTR(L) STR(cstring, L) template<char …C> struct cstring { }; template<template<char…> class P, typename S, typename … Read more

Pseudo-generics in C

You can do something like this in a header file: // // generic.h // #define TOKENPASTE(x, y) x ## y #define GET_MINIMUM(T) TOKENPASTE(get_minimum_, T) TYPE GET_MINIMUM (TYPE) (TYPE * nums, size_t len){ TYPE min = nums[0]; for (size_t i = 1; i < len; i++) { if (nums[i] < min) { min = nums[i]; } … Read more

What does ## in a #define mean?

A little bit confused still. What will the result be without ##? Usually you won’t notice any difference. But there is a difference. Suppose that Something is of type: struct X { int x; }; X Something; And look at: int X::*p = &X::x; ANALYZE(x, flag) ANALYZE(*p, flag) Without token concatenation operator ##, it expands … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)