Skip to content

Commit

Permalink
<print> : print, printlnを追加 #1082
Browse files Browse the repository at this point in the history
  • Loading branch information
faithandbrave committed Jan 30, 2023
1 parent 6d029cb commit 469297f
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 2 deletions.
4 changes: 2 additions & 2 deletions reference/print.md
Expand Up @@ -9,8 +9,8 @@

| 名前 | 説明 | 対応バージョン |
|-----------------|----------------|-------|
| [`print`](print/print.md.nolink) | 書式指定で出力する (function template) | C++23 |
| [`println`](print/println.md.nolink) | 書式指定で出力する。末尾改行付き (function template) | C++23 |
| [`print`](print/print.md) | 書式指定で出力する (function template) | C++23 |
| [`println`](print/println.md) | 書式指定で出力する。末尾改行付き (function template) | C++23 |
| [`vprint_unicode`](print/vprint_unicode.md.nolink) | 書式指定でUnicode出力する (function template) | C++23 |
| [`vprint_nonunicode`](print/vprint_nonunicode.md.nolink) | 書式指定で非Unicode出力する (function template) | C++23 |

Expand Down
119 changes: 119 additions & 0 deletions reference/print/print.md
@@ -0,0 +1,119 @@
# print
* print[meta header]
* std[meta namespace]
* function template[meta id-type]
* cpp23[meta cpp]

```cpp
namespace std {
template <class... Args>
void print(format_string<Args...> fmt,
Args&&... args); // (1) C++23

template <class... Args>
void print(FILE* stream,
format_string<Args...> fmt,
Args&&... args); // (2) C++23
}
```
* format_string[link /reference/format/format_string.md.nolink]
* FILE[link /reference/cstdio/file.md.nolink]
## 概要
書式指定で出力する。
書式は[`std::format()`](/reference/format/format.md)関数のページを参照。
この関数は、[`std::printf()`](/reference/cstdio/printf.md.nolink)関数ライクな書式指定で引数を文字列化して出力する。
- (1) : 標準出力に、書式指定で出力する
- (2) : 指定された[`FILE`](/reference/cstdio/file.md.nolink)に、書式指定で出力する
この関数は、末尾に改行コードが付かないことに注意。改行コードを自動で付けたい場合は、[`std::println()`](println.md)関数を使用すること。
[`std::ostream`](/reference/ostream/basic_ostream.md)から派生したクラスオブジェクトに対して出力したい場合は、[`<ostream>`](/reference/ostream.md)ヘッダの[`std::print()`](/reference/ostream/print.md.nolink)関数を使用すること。
## 効果
- (1) : 以下と等価:
```cpp
print(stdout, fmt, std::forward<Args>(args)...);
```
* stdout[link /reference/cstdio/stdout.md.nolink]
* std::forward[link /reference/utility/forward.md]

- (2) : 通常の文字列リテラルがUTF-8エンコーディングされている場合、以下と等価:
```cpp
vprint_unicode(stream, fmt.get(), make_format_args(std::forward<Args>(args)...));
```
* vprint_unicode[link vprint_unicode.md.nolink]
* fmt.get()[link /reference/format/format_string/get.md.nolink]
* make_format_args[link /reference/format/make_format_args.md]
* std::forward[link /reference/utility/forward.md]

- そうでなければ、以下と等価:
```cpp
vprint_nonunicode(stream, fmt.get(), make_format_args(std::forward<Args>(args)...));
```
* vprint_nonunicode[link vprint_nonunicode.md.nolink]
* fmt.get()[link /reference/format/format_string/get.md.nolink]
* make_format_args[link /reference/format/make_format_args.md]
* std::forward[link /reference/utility/forward.md]


## 備考
- LinuxやmacOSといった環境では、通常の`char`配列の文字列リテラルはUTF-8にエンコーディングされる
- WindowsのVisual Studioにおいては、ソースコードと実行時文字集合をUTF-8にする[`/utf-8`オプション](https://learn.microsoft.com/en-us/cpp/build/reference/utf-8-set-source-and-executable-character-sets-to-utf-8?view=msvc-170)を使用することで、通常の`char`配列の文字列リテラルがUTF-8にエンコーディングされる
- 「通常の文字列リテラルがUTF-8エンコーディングされている場合」という仕様は、コードでは以下のように表現できる:
```
constexpr bool is_utf8() {
const unsigned char micro[] = "\u00B5";
return sizeof(micro) == 3 && micro[0] == 0xC2 && micro[1] == 0xB5;
}
template <typename... Args>
void print(string_view fmt, const Args&... args) {
if (is_utf8())
vprint_unicode(fmt, make_format_args(args...));
else
vprint_nonunicode(fmt, make_format_args(args...));
}
```
* vprint_unicode[link vprint_unicode.md.nolink]
* vprint_nonunicode[link vprint_nonunicode.md.nolink]


##
```cpp example
#include <print>

int main()
{
std::print("Hello {} World\n", 42);
}
```
* std::print[color ff0000]

### 出力
```
Hello 42 World
```

## バージョン
### 言語
- C++23

### 処理系
- [Clang](/implementation.md#clang): ??
- [GCC](/implementation.md#gcc): ??
- [ICC](/implementation.md#icc): ??
- [Visual C++](/implementation.md#visual_cpp): ??


## 関連項目
- [`std::format()`](/reference/format/format.md)
- [`std::println()`](println.md)


## 参照
- [P2093R14 Formatted output](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2093r14.html)
87 changes: 87 additions & 0 deletions reference/print/println.md
@@ -0,0 +1,87 @@
# println
* print[meta header]
* std[meta namespace]
* function template[meta id-type]
* cpp23[meta cpp]

```cpp
namespace std {
template <class... Args>
void println(format_string<Args...> fmt,
Args&&... args); // (1) C++23

template <class... Args>
void println(FILE* stream,
format_string<Args...> fmt,
Args&&... args); // (2) C++23
}
```
* format_string[link /reference/format/format_string.md.nolink]
* FILE[link /reference/cstdio/file.md.nolink]
## 概要
書式指定で出力する。この関数は、出力の末尾に改行コードが自動で付加される。
書式は[`std::format()`](/reference/format/format.md)関数のページを参照。
この関数は、[`std::printf()`](/reference/cstdio/printf.md.nolink)関数ライクな書式指定で引数を文字列化して出力する。
- (1) : 標準出力に、書式指定で出力する
- (2) : 指定された[`FILE`](/reference/cstdio/file.md.nolink)に、書式指定で出力する
この関数は、末尾に改行コードが付くことに注意。改行コードが不要な場合は、[`std::print()`](print.md)関数を使用すること。
[`std::ostream`](/reference/ostream/basic_ostream.md)から派生したクラスオブジェクトに対して出力したい場合は、[`<ostream>`](/reference/ostream.md)ヘッダの[`std::print()`](/reference/ostream/println.md.nolink)関数を使用すること。
## 効果
- (1) : 以下と等価:
```cpp
println(stdout, fmt, std::forward<Args>(args)...);
```
* stdout[link /reference/cstdio/stdout.md.nolink]
* std::forward[link /reference/utility/forward.md]

- (2) : 以下と等価:
```cpp
print(stream, "{}\n", format(fmt, std::forward<Args>(args)...));
```
* print[link print.md]
* format[link /reference/format/format.md]
* std::forward[link /reference/utility/forward.md]


##
```cpp example
#include <print>

int main()
{
std::println("Hello {} World", 42);
}
```
* std::println[color ff0000]

### 出力
```
Hello 42 World
```

## バージョン
### 言語
- C++23

### 処理系
- [Clang](/implementation.md#clang): ??
- [GCC](/implementation.md#gcc): ??
- [ICC](/implementation.md#icc): ??
- [Visual C++](/implementation.md#visual_cpp): ??


## 関連項目
- [`std::format()`](/reference/format/format.md)
- [`std::print()`](print.md)


## 参照
- [P2093R14 Formatted output](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2093r14.html)

0 comments on commit 469297f

Please sign in to comment.