c++

C++でバックトレース(スタックトレース)を表示する方法(C++23)

C++23を使うと、スタックトレースが簡単に表示できます #include <stacktrace> #include <iostream> void f() { std::cout << std::stacktrace::current() << std::endl; } int main() { f(); return 0; } C++23で登場した関数なので GCCはバージョン12以降 Visual C++は2022以降 </iostream></stacktrace>…

C++20の concepts を使ったSFINAE

C++20でやっとSFINAE(Substitution Failure Is Not An Error)が実用レベル(?)になったので,まとめておきますC++20版のSFINAEだけでなく,C++17版やC++11以前版のSFINAEも用意しました. 見比べると如何にC++11がクソで,C++20が素晴らしいかが一目で理…

メンバーへのポインタ(c++, .* と ->*演算子 )

c++

c++のメンバへのポインタの使い方をまとめます 利用例 たとえば以下のデータ構造について struct Employee { int id; std::string name; double salary; }; std::vector<Employee> employees; std::sort()で Employee::id をキーにソートしたい場合を考えますこの場合</employee>…

可変長テンプレートでパラメータ・パックを2つ同時に使う方法

c++の可変長テンプレートで2つの型リストKeysとValuesを使いたいとする その場合は リストの片方は std::tupleで一つの型にする 残りのリストは 可変長テンプレートにする とコーディングしやすい 例 template <typename KeyTuple, typename... Values_> struct Foo { using Keys = KeyTuple; using </typename>…

c++のPerfect Forwarding(完全転送,std::forward)の使い方

c++

c++11 の perfect forwading (完全転送,std::forward)をつかうと,たとえば派生クラスの実装が簡潔かつ安全に実装できるようになります. 例:派生クラスのコンストラクタから,基底クラスのコンストラクタへ引数を完全転送する 基底クラスBaseを派生する…

条件付きコンパイルでC++標準のバージョンを調べる方法

c++

条件付きコンパイルで、C++標準のバージョンを調べるときは __cplusplus プリプロセッサマクロを使いますたとえば以下のコードは、c++のコンパイラが c++11 に準拠していない場合、コンパイルエラーとなります #if __cplusplus < 201103L # error "C++11に対…

std::string と std::wstring の相互変換

c++

c++ で std::string と std::wstring (ワイド文字列版) を相互に変換するコードですプロトタイプ宣言 #include <string> std::wstring s2ws(const std::string& str); std::string ws2s(const std::wstring& wstr); 実装 #include <string> #include <codecvt> std::wstring s2ws(cons</codecvt></string></string>…

SFINAEでオーバーロードされた関数を検出する方法

c++

C++ のSFINAE (Substitution Failure Is Not An Error)を使って,クラス T に foo(int a, int b)があるか否かを調べる方法です. 簡単な例(class Tに オーバーロードされた関数がない場合) まずはシンプルに,クラス T に対してT::foo が存在するかどうか調…

C++/Eigen で特異値分解(SVD)を使って一般化逆行列を計算する

一般化逆行列(擬似逆行列,最小二乗法に対応するやつ)をc++で計算するサンプル特異値分解(SVD)が必要なので,実装は Eigen を使いますpython/numpy の実装も用意しました.こちらです https://pyopyopyo.hatenablog.com/entry/2021/09/14/161955 #include <Eigen/Dense></eigen/dense>…

/dev/null みたいな std::ostream

c++

/dev/null みたいな std::ostream を作る方法. 方法1 : std::ostreamを継承して自前の null stream を作成する 以下のURLで紹介されている方法 stackoverflow.com #include <iostream> class NulStreambuf : public std::streambuf { char dummyBuffer[ 64 ]; protect</iostream>…

コンパイラのAddressSanitizerを使ってバッファ オーバーフローを退治する

C言語やC++の欠点の一つは,バッファオーバーフローやオーバーランが起こりやすいことです.それは欠点であると同時にC/C++の利点であると言う人もいます.つまり,メモリ関連のチェックを省略することで,省略した分C/C++はオーバヘッドが少ない高速なプロ…

iostream や std::stringでprintfのような書式指定を行う方法 (C++11版)

C++11を使うと綺麗に実装できます まず format() というテンプレート関数を作ります.C++11で新しく導入された,可変引数テンプレート,および std::snprintf() を使います. #include <string> #include <cstdio> #include <vector> template <typename ... Args> std::string format(const std::string</typename></vector></cstdio></string>…

class を noncopyable にする

c++

今時のc++ (c++11など)でコピー禁止,代入禁止のクラスを作る方法 template <class T> struct CRTPnoncopyable { CRTPnoncopyable(const CRTPnoncopyable&) = delete; CRTPnoncopyable& operator=(const CRTPnoncopyable &) = delete; }; class A : CRTPnoncopyable<A> </a></class>…

ポインタからクラス名を調べる方法

c++

c++で、ポインタからクラス名や型名を調べる処理は、以下の2ステップで実現できます typeid(obj).name() で型名の文字列を取る abi::__cxa_demangle() で demangle する サンプルコードは以下の通り #include <typeinfo> #include <cxxabi.h> #include <iostream> int main() { class MyCl</iostream></cxxabi.h></typeinfo>…

c++でプログレス・バーを表示する方法

boost の boost/progress.hpp を使えばOK #include <boost/progress.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/thread/thread.hpp> int main(int argc,char *argv[]) { const unsigned long expected_count=100; boost::progress_display show_progress( expected_count ); for(int i=0; i</boost/thread/thread.hpp></boost/date_time/posix_time/posix_time.hpp></boost/progress.hpp>

boost 1.50.0 移行メモ

boost-1.50.0 へ乗り換えました.移行方法をメモします. 1.50.0 での変更箇所 http://www.boost.org/users/history/version_1_50_0.html に一覧がありますが,機能追加・バグ修正・仕様変更と色々変わっています.特に大きな変更点としては,boost::filesys…