3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

c++23の模範的なHelloWorld

Last updated at Posted at 2023-04-21
hello.cpp
// g++ -std=c++20 -fmodules-ts hello.cpp -o hello.exe -x c++-system-header iostream -Wall -Wextra -Werror -pedantic -O3

import <iostream>;

int main( ) {
  std::cout << "Hello world!" << std::endl;
  return 0;
}

// 今からc++するなら最初のコードはこれであるべき

前置き

2023/4/22

twitterに上げたやつを軽く手直し+簡単な説明
※g++のバージョンは最新にしましょう

2023/6/13

そういえば、std::printが追加されるとかいう話を風のうわさで聞きました

説明

コンパイルのオプション

  • -std=c++20
    c++20を指定

  • -fmodules-ts
    モジュールを使えるようにするやつ

  • hello.cpp
    対象指定

  • -o hello.exe
    出力ファイル名

  • -x c++-system-header iostream
    g++ -x c++-system-header
    って感じでコンパイルするやつ

  • -Wall -Wextra -Werror
    警告を有効+さらなる警告を有効+警告をエラーとして扱う

  • -pedantic
    言語規格に厳密に従う
    C++規格で許容されていない機能を使用できなくする

  • -O3
    最適化、レベル3
    インライン関数の展開を有効化など
    ※IDEのデバッガなどがブレークポイントを設定できなかったりするため、最適化しない限りインライン関数は展開されない
    また、似たものとして

    • -funroll-loops
      ループを展開
    • -finline-functions
      インライン関数を展開

    がある。

本文

  • import ;
    c++20で追加された
    インクルードの順番でコンパイルできないなんてことがなくなる

  • std::cout << "Hello world!" << std::endl;
    全角のエクスクラメーションマークを使っている
    これで文字化けして出力されるようなら君のwindowsのコンソールはutf8になっていない
    下記でも参考にして変えよう

  • return 0;
    実行が正常に終了したことを示している。

よくある1~1000の素数を出力するやつをいい感じにしたかった

※c++23のものを使ったせいか、そもそもまだ登録されていない関数っぽいものがあり、コンパイルできないです。clangはよくわかりませんでした

※途中で力尽きたので素数を生成しません

PrimeNumber.cpp
// ヘッダーユニットをコンパイル
// g++ -fmodules-ts -std=c++23 -x c++-system-header ranges
// g++ -fmodules-ts -std=c++23 -x c++-system-header iostream
// g++ -fmodules-ts -std=c++23 -x c++-system-header vector
// g++ -fmodules-ts -std=c++23 -x c++-system-header iterator
// g++ -fmodules-ts -std=c++23 -x c++-system-header algorithm
// g++ -fmodules-ts -std=c++23 -x c++-system-header bitset
// g++ -fmodules-ts -std=c++23 -x c++-system-header string_view
// // g++ -fmodules-ts -std=c++23 -x c++-system-header concepts
// g++ -std=c++23 -fmodules-ts PrimeNumber.cpp -o PrimeNumber.exe -Wall -Wextra -Werror -pedantic -O3

// clang -fmodules-ts --precompile -std=c++23 -x c++-system-header ranges
// clang -fmodules-ts --precompile -std=c++23 -x c++-system-header iostream
// clang -fmodules-ts --precompile -std=c++23 -x c++-system-header vector
// clang -fmodules-ts --precompile -std=c++23 -x c++-system-header iterator
// clang -fmodules-ts --precompile -std=c++23 -x c++-system-header algorithm
// clang -fmodules-ts --precompile -std=c++23 -x c++-system-header bitset
// clang -fmodules-ts --precompile -std=c++23 -x c++-system-header string_view
// // clang -fmodules-ts --precompile -std=c++23 -x c++-system-header concepts
// clang -std=c++2b -fmodules-ts PrimeNumber.cpp -o PrimeNumber.exe -Wall -Wextra -Werror -pedantic -O3 # import先が認識されない

import <iostream>;
import <ranges>;
import <vector>;
import <iterator>;
import <algorithm>;
import <bitset>;
import <string_view>;
// import <concepts>;

int main( ) {
  const int      n = 1'000;
  std::bitset<n> is_prime;

  std::fill_n(is_prime[0], is_prime.size( ), 1);

  for (constexpr auto elem : std::views::iota(0, is_prime.size( )) | std::views::stride(3)) {
    is_prime.set(elem, false);
  };

  // // Use C++23's std::ranges::subrange with std::ranges::views::filter
  auto primes_range = std::ranges::subrange(std::views::iota(0, is_prime.size( )) | std::ranges::views::filter([](bool b) {
                                              return b;
                                            }));

  std::cout << "There are primes from 1 to 1,000:" << std::endl;
  for (const auto [index, number] : std::numbers | std::views::enumerate) {
    if (is_prime[number]) {
      std::cout << "Prime number #" << index + 1 << ": " << number << std::endl;
    }
  }
  return 0;
}

参考

※全部ではないです

AI補助

他に普段は使用しているものもありますが、今回使ったのはこれだけでs

bingai

3
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?