Featured

How to trace/debug if g++ doesn’t like your templates based code

#include <typeinfo>
#include <cxxabi.h>
#include <cstdlib>
#include <memory>
#include <string>

template <typename Type>
std::string TypeName()
{
    //https://en.wikipedia.org/wiki/Name_mangling
    //https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a01696.html

    auto demangle=[](char const* mangled) -> std::string {
        auto ptr = std::unique_ptr<char, decltype(& std::free)>{
            abi::__cxa_demangle(mangled, nullptr, nullptr, nullptr),
            std::free
        };
        return {ptr.get()};
    };
    return demangle(typeid(Type).name());
}
//...
template <typename F, typename ...Args>
void //typename std::enable_if<std::is_same<...   <---------------------if something worng is here
foo()
{
    std::cout << "line: "<< __LINE__ <<": "<<TypeName<F(Args...)>() << std::endl;
    std::cout << "line: "<< __LINE__ <<": "<<TypeName<F(char,Args...)>() << std::endl;
    std::cout << "line: "<< __LINE__ <<": "<<TypeName<typename std::result_of<F(char,Args...)>::type>() << std::endl;
}

#c++ #templates

Featured

compile a directory & run

files="" && ls -1 *.c|while read line; do files="$line $files"; done && $(gcc -Wall $(echo $files) -o bin.x) && echo -ne "Running bin.x\n" && ./bin.x > bin.log &

zsh saves variables. bash doesn’t.

files="" && ls -1 *.c|while read line; do files="$files $line"; echo $files > files.lst; done && gcc -v -Wall `tail -n 1 files.lst` -o bin.x && echo -ne "Running bin.x\n" && ./bin.x

Featured

Redirect output to a file without buffering on linux/mac?

https://serverfault.com/questions/294218/is-there-a-way-to-redirect-output-to-a-file-without-buffering-on-unix-linux

https://serverfault.com/questions/294218/is-there-a-way-to-redirect-output-to-a-file-without-buffering-on-unix-linux

This buffers stdout up to a line:

stdbuf -oL command > output

This disables stdout buffering altogether:

stdbuf -o0 command > output

This disables stdout buffering altogether:

stdbuf -o0 command > output
//setbuf(stdout, NULL);
//fflush(stdout); // Will now print everything in the stdout buffer

stdbuf -i0 -o0 -e0 ./bin.x > bin.log
stdbuf -i0 -o0 -e0 ./bin.x | sed -n 'w bin.log' & tail -f bin.log

Why is it safer to use sizeof(*pointer) in malloc

https://stackoverflow.com/a/17258659

It is safer becuse you don’t have to mention the type name twice and don’t have to build the proper spelling for “dereferenced” version of the type. For example, you don’t have to “count the stars” in

int *****p = malloc(100 * sizeof *p);

Compare that to the type-based sizeof in

int *****p = malloc(100 * sizeof(int ****));

where you have too make sure you used the right number of * under sizeof.

In order to switch to another type you only have to change one place (the declaration of p) instead of two. And people who have the habit of casting the result of malloc have to change three places.