前提・実現したいこと
c++でコマンドライン引数const char*をconst intにキャストしたいです.
なぜならstd::bitsetの宣言時のテンプレート引数intをコマンドライン引数から指定したいためです.
発生している問題・エラーメッセージ
$ g++ -o test test.cpp -std=gnu++17 test.cpp: In function ‘int main(int, const char**)’: test.cpp:7:12: error: the value of ‘N’ is not usable in a constant expression bitset<N> bits; ^ test.cpp:6:21: note: ‘N’ was not initialized with a constant expression const int N = atoi(argv[1]); ^ test.cpp:7:13: error: the value of ‘N’ is not usable in a constant expression bitset<N> bits; ^ test.cpp:6:21: note: ‘N’ was not initialized with a constant expression const int N = atoi(argv[1]); ^ test.cpp:7:13: note: in template argument for type ‘long unsigned int’ bitset<N> bits;
該当のソースコード
c++
1#include <bitset>//ビット演算 2using namespace std; 3 4int main(int argc, const char *argv[]){ 5 //定数等の宣言 6 const int N = atoi(argv[1]); 7 bitset<N> bits; 8 9}
試したこと
static_castによるconst intへのキャスト, atoiにconstexprの返り値がないか確認.
補足情報(FW/ツールのバージョンなど)
$ g++ -dumpversion 5.4.0
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/22 09:41