[c++] #define __atribute__

– The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.


__attribute__((constructor)) is a GCC compiler extension in C++ that allows you to specify a function that should be executed automatically before the main function of your program.

[c++] ifdefディレクティブ

条件付きコンパイラ
#ifdef および #ifndef のディレクティブは、#if を使用できるところならどこでも使用できる。

#defineはプリプロセッサ(コンパイル前の処理)を行う

[c++] mutex

mutexはスレッド間で使用する共有リソースを排他制御するためのクラス
lock()メンバ関数によってロックを取得し、unlock()メンバ関数でリソースのロックを手放す

#include <iostream>
#include <thread>
#include <mutex>
#include <vector>

class X {
    std::mutex mtx_;
    std::vector<int> data_;

    public:
        void add_value(int value){
            std::lock_guard<std::mutex> lock(mtx_);
            data_.push_back(value);
        }

        void print(){
            for(int x: data_){
                std::cout << x << std::endl;
            }
        };
}

int main() {
    X x;

    std::thread t1([&x]{x.add_value(1);})
    std::thread t2([&x]{x.add_value(2);})

    t1.join();
    t2.join();

    x.print();
}

[c++] explicitとは?

構造体やclassのコンストラクタにつけることで暗黙的な型変換を防止する機能

struct A {
    explicit A(int);
};
struct B {
    B(A) {}
};

int main(){
    // B b(1);
    return 0;
}

型変換できない

[c++] int64_t

64ビットの符号付き整数型
64ビット固定の場合はlong longよりも推奨される

int64_t x = 0LL;

[C++] constexpr

constexprを使わないケース
### 変数
– constでない変数
– クラスのメンバ変数
– 標準入力などの非constexpr関数を用いて計算する値
– 引数などのconstexprでない可能性がある値を用いて計算する値
### 関数
– inline化できない関数
– 引数でもthisでもない非constexprな外側の変数を参照する操作を含む関数
– 引数でもthisでもない外側に副作用を及ぼすような操作を含む関数


#include

auto answer() {return 42;}

constexpr auto auto_constexpr() {return 42;}

constexpr auto answer_print(){
return 42;
}

int main(){
const expr auto b = answer_constexpr();

std::cout << /* a << ", " << */ b << /* ", " << c << */ std::endl; return 0; } [code]

C++ mapの使い方

key, valueの連想配列class

struct Person {
    public:
        Person(const string &name, int height)
            : m_name(name)
            , m_height(height)
            {}
    public:
        string m_name;
        int m_height;
}

    ...
    map<Person, int> mp;
    Person p("aaa", 180);
    mp[p] = 123;

c++ cstdint

符号付き、符号なしで型の指定ができる

int8_t、int16_t、int32_t、int64_t、uint8_t、uint16_t、uint32_t、uint64_t、int_fast8_t、int_fast16_t、int_fast32_t、int_fast64_t、uint_fast8_t、uint_fast16_t、uint_fast32_t、uint_fast64_t、int_least8_t、int_least16_t、int_least32_t、int_least64_t、uint_least8_t、uint_least16_t、uint_least32_t、uint_least64_t、intmax_t、uintmax_t、intptr_t、uintptr_t

それにしても多いですね

CMakeの使い方2

good_morning.hpp

#ifndef GOOD_MORNING_H
#define GOOD_MORNING_H

void good_morning();

#endif

good_morning.cpp

#include <iostream>
#include "good_morning.hpp"

void good_morning() {
    std::cout << "Good Monring!" << std::endl;
}

main.cpp

#include "hello.hpp"
#include "good_morning.hpp"

int main(){
    hello();
    good_morning();
}

$ g++ -c hello.cpp good_morning.cpp
$ ar rvs libgreetings.a hello.o good_morning.o
ar: creating libgreetings.a
a – hello.o
a – good_morning.o
$ g++ main.cpp libgreetings.a
$ ./a.out
Hello!
Good Monring!