std::optionalとは?モダンC++が提供する究極のNull安全機能
従来のNullポインタが引き起こす危険な問題とは
C++開発において、値が存在しない可能性を表現する方法として長年使用されてきたNullポインタには、深刻な問題が潜んでいます。以下のコード例で具体的に見ていきましょう:
// 危険なNullポインタの例
std::string* getUserName(int userId) {
if (userId <= 0) {
return nullptr; // 無効なユーザーIDの場合
}
// データベースからユーザー名を取得する処理
return new std::string("John Doe");
}
void processUserName() {
std::string* name = getUserName(-1);
std::cout << name->length(); // 危険!NULLポインタ参照による未定義動作
delete name; // メモリリーク or 二重解放の可能性
}
このコードには以下の問題があります:
- Nullポインタの参照外しによる未定義動作
- メモリ管理の責任が呼び出し側に転嫁される
- Nullチェックの忘れによるバグの発生
- エラー状態の意図が不明確
std::optionalによって実現される安全な値の取り扱い
C++17で導入されたstd::optionalは、これらの問題を解決する優れた代替手段を提供します:
#include <optional>
#include <string>
// std::optionalを使用した安全な実装
std::optional<std::string> getUserName(int userId) {
if (userId <= 0) {
return std::nullopt; // 値が存在しない状態を明示的に表現
}
return std::string("John Doe"); // 値が存在する場合
}
void processUserName() {
auto name = getUserName(-1);
if (name) { // 値の存在確認が強制される
std::cout << name->length(); // 安全な参照
}
// 明示的なメモリ管理が不要
}
std::optionalの主な利点:
- 型安全性の保証
- コンパイル時の型チェック
- 暗黙的な型変換の防止
- 値の存在確認が強制される
- 明示的な意図の表現
- 関数の戻り値型から「値が存在しない可能性」が明確
- APIの契約としての役割
- コードの自己文書化
- メモリ安全性の向上
- スタック上に直接オブジェクトを保持
- RAII原則に従った自動的なリソース管理
- メモリリークの心配が不要
- パフォーマンスの最適化
- 動的メモリ確保が不要
- 値型のセマンティクス
- 最適化の余地が大きい実装
std::optionalは以下のような状況で特に有効です:
- 検索操作の結果が存在しない可能性がある場合
- 設定値やパラメータがオプショナルである場合
- 初期化が遅延される可能性がある場合
- エラー状態をnullableな値として表現したい場合
これにより、より安全で保守性の高いC++コードを書くことができます。続くセクションでは、std::optionalの具体的な使用方法と実践的なパターンについて詳しく見ていきましょう。
std::optionalの基本的な使い方をマスターする
std::optionalオブジェクトの生成と初期化テクニック
std::optionalの初期化には複数の方法があります。以下で代表的な初期化パターンを見ていきましょう:
#include <optional>
#include <string>
void demonstrateInitialization() {
// 空のoptionalを作成
std::optional<int> empty; // 値を持たないoptional
// 値を持つoptionalを作成
std::optional<int> withValue = 42; // 直接値で初期化
std::optional<std::string> withString{"Hello"}; // 文字列で初期化
// make_optionalを使用した初期化
auto constructed = std::make_optional<std::string>("World");
// コンストラクタ引数を渡して初期化
auto inPlace = std::optional<std::string>(std::in_place, 5, 'x'); // "xxxxx"
// nulloptを使用した明示的な空の初期化
std::optional<double> explicit_empty = std::nullopt;
}
値の存在確認と安全な取り出し方法
std::optionalは値の存在確認と安全な取り出しのための様々なメソッドを提供します:
void demonstrateValueAccess(const std::optional<std::string>& opt) {
// 値の存在確認
if (opt.has_value()) { // または if (opt)
std::cout << "値が存在します: " << opt.value() << std::endl;
}
// 値の安全な取り出し
try {
std::string val = opt.value(); // 値が存在しない場合は例外をスロー
} catch (const std::bad_optional_access& e) {
std::cout << "値が存在しません" << std::endl;
}
// アロー演算子を使用したメンバーアクセス
if (opt) {
std::cout << "文字列の長さ: " << opt->length() << std::endl;
}
// value_or()を使用したデフォルト値の指定
std::string result = opt.value_or("デフォルト値");
}
デフォルト値を使用した柔軟な値の取り扱い
std::optionalを使用する際の実践的なパターンをいくつか見ていきましょう:
class Configuration {
std::optional<int> port;
std::optional<std::string> host;
public:
// デフォルト値を使用した設定値の取得
int getPort() const {
return port.value_or(8080); // ポートが未設定の場合は8080を使用
}
// 条件付きの値設定
void setPort(int newPort) {
port = (newPort > 0 && newPort < 65536) ?
std::optional<int>(newPort) : std::nullopt;
}
// 値の存在に応じた処理分岐
void connectToHost() {
if (host && port) {
std::cout << "接続: " << *host << ":" << *port << std::endl;
} else {
std::cout << "接続情報が不完全です" << std::endl;
}
}
};
実践的なユースケース例:
// ユーザー入力の検証と変換
std::optional<int> parseUserInput(const std::string& input) {
try {
int value = std::stoi(input);
return (value >= 0) ? std::optional<int>(value) : std::nullopt;
} catch (...) {
return std::nullopt; // 変換失敗時は空のoptionalを返す
}
}
// キャッシュシステムでの使用例
class Cache {
std::unordered_map<std::string, std::optional<std::string>> cache;
public:
std::optional<std::string> get(const std::string& key) {
auto it = cache.find(key);
if (it != cache.end()) {
return it->second; // キャッシュされた値(nulloptの可能性あり)
}
return std::nullopt; // キーが存在しない
}
void set(const std::string& key, const std::optional<std::string>& value) {
cache[key] = value;
}
};
使用時の重要なポイント:
- 初期化の選択
- 用途に応じて適切な初期化方法を選択
std::nulloptを使用して明示的に空の状態を表現std::in_placeを使用して効率的な構築
- 値のアクセス
has_value()または暗黙的な変換で存在確認value()またはoperator*で値にアクセスvalue_or()でデフォルト値を指定
- 例外処理
value()使用時の例外ハンドリングを考慮- 必要に応じて
value_or()を使用して例外を回避
- パフォーマンス考慮
- 不必要なコピーを避けるための参照の使用
- 適切な場面での
emplace()の活用
これらの基本的な使い方を理解することで、std::optionalを効果的に活用できるようになります。次のセクションでは、より高度な活用パターンについて見ていきましょう。
実践的なstd::optionalの活用パターン
関数の戻り値としての効果的な使用方法
関数の戻り値としてstd::optionalを使用することで、エラーハンドリングをより表現力豊かに実装できます:
#include <optional>
#include <string>
#include <vector>
#include <algorithm>
// データベース検索を模したクラス
class UserDatabase {
std::vector<std::pair<int, std::string>> users;
public:
UserDatabase() {
users = {{1, "Alice"}, {2, "Bob"}, {3, "Charlie"}};
}
// 検索結果が存在しない可能性を明示的に表現
std::optional<std::string> findUserById(int id) {
auto it = std::find_if(users.begin(), users.end(),
[id](const auto& pair) { return pair.first == id; });
if (it != users.end()) {
return it->second;
}
return std::nullopt;
}
// 複数の条件を組み合わせた検索
std::optional<int> findIdByName(const std::string& name) {
auto it = std::find_if(users.begin(), users.end(),
[&name](const auto& pair) { return pair.second == name; });
return it != users.end() ?
std::optional<int>(it->first) : std::nullopt;
}
};
条件付き値の取り扱いにおける活用例
複雑な条件分岐や値の変換を伴う処理での活用例を見てみましょう:
class ConfigParser {
public:
// 設定値の型安全な変換
template<typename T>
static std::optional<T> parseValue(const std::string& str) {
try {
if constexpr (std::is_same_v<T, int>) {
return std::stoi(str);
} else if constexpr (std::is_same_v<T, double>) {
return std::stod(str);
} else if constexpr (std::is_same_v<T, bool>) {
return str == "true" || str == "1";
}
} catch (...) {
return std::nullopt;
}
return std::nullopt;
}
// 条件付きの値検証と変換
static std::optional<int> validatePort(const std::string& str) {
auto port = parseValue<int>(str);
if (port && *port > 0 && *port < 65536) {
return port;
}
return std::nullopt;
}
};
// 使用例
void demonstrateConfigParsing() {
auto port = ConfigParser::validatePort("8080");
if (port) {
std::cout << "有効なポート: " << *port << std::endl;
}
}
チェーン操作による優雅なコードの実現
std::optionalを使用したチェーン操作パターンを実装することで、より読みやすく保守性の高いコードを実現できます:
template<typename T>
class Optional {
std::optional<T> value;
public:
Optional(const std::optional<T>& v) : value(v) {}
// 値が存在する場合のみ変換を適用
template<typename Func>
auto map(Func f) const {
using ResultType = decltype(f(std::declval<T>()));
if (value) {
return Optional<ResultType>(f(*value));
}
return Optional<ResultType>(std::nullopt);
}
// 条件に基づくフィルタリング
Optional<T> filter(std::function<bool(const T&)> pred) const {
if (value && pred(*value)) {
return *this;
}
return std::nullopt;
}
// 値の取得
const std::optional<T>& get() const { return value; }
};
// 実践的な使用例
class UserService {
UserDatabase db;
public:
struct UserInfo {
std::string name;
int age;
};
Optional<UserInfo> getUserInfo(int id) {
return Optional(db.findUserById(id))
.map([](const std::string& name) {
return UserInfo{name, 30}; // 年齢は簡略化のため固定
})
.filter([](const UserInfo& info) {
return info.age >= 18; // 成人ユーザーのみ
});
}
};
実践的な活用パターンのまとめ:
- 戻り値としての使用
- エラー状態の明示的な表現
- 型安全な結果の返却
- コード契約の明確化
- 条件付き処理
- 型変換の安全な実装
- 入力値の検証
- 複雑な条件分岐の簡略化
- チェーン操作
- 関数型プログラミングスタイルの実現
- コードの可読性向上
- 処理の組み合わせ容易性
これらのパターンを適切に組み合わせることで、より表現力豊かで保守性の高いコードを実現できます。次のセクションでは、これらのパターンを実装する際のパフォーマンスとメモリ管理について詳しく見ていきましょう。
std::optionalのパフォーマンスとメモリ管理
内部実装と最適化のメカニズム
std::optionalの内部実装を理解することで、より効率的な使用方法が見えてきます:
// std::optionalの簡略化された内部実装イメージ
template<typename T>
class optional {
private:
// アライメント要件を満たすストレージ
alignas(T) unsigned char storage[sizeof(T)];
bool has_value_; // 値の存在フラグ
// 実際の実装ではもっと複雑な最適化が行われています
};
主な実装特性:
- メモリレイアウト
- 値型のインライン保持
- アライメント要件の自動処理
- 追加のメモリオーバーヘッドは最小限
- 最適化機会
- コンパイラによる積極的なインライン化
- 空の最適化(Empty Base Optimization)の活用
- ムーブセマンティクスの効率的な実装
パフォーマンス測定例:
#include <chrono>
#include <optional>
#include <memory>
// パフォーマンス比較用の関数
void comparePerformance() {
const int iterations = 1000000;
// std::optionalのパフォーマンス測定
auto start = std::chrono::high_resolution_clock::now();
std::optional<std::string> opt;
for (int i = 0; i < iterations; ++i) {
opt = std::string("test");
if (opt) {
opt = std::nullopt;
}
}
auto end = std::chrono::high_resolution_clock::now();
auto optional_duration = std::chrono::duration_cast<std::chrono::microseconds>
(end - start).count();
// 生ポインタのパフォーマンス測定
start = std::chrono::high_resolution_clock::now();
std::string* ptr = nullptr;
for (int i = 0; i < iterations; ++i) {
ptr = new std::string("test");
if (ptr) {
delete ptr;
ptr = nullptr;
}
}
end = std::chrono::high_resolution_clock::now();
auto pointer_duration = std::chrono::duration_cast<std::chrono::microseconds>
(end - start).count();
std::cout << "std::optional: " << optional_duration << "µs\n";
std::cout << "raw pointer: " << pointer_duration << "µs\n";
}
メモリ使用量の最適化テクニック
効率的なメモリ使用のためのベストプラクティス:
class MemoryEfficientExample {
private:
// 大きなオブジェクトに対する最適化
struct LargeObject {
std::array<char, 1024> data;
std::string metadata;
};
// 必要になった時点で初期化
std::optional<LargeObject> large_object;
public:
// 遅延初期化の実装
void initializeLazy() {
if (!large_object) {
large_object.emplace(); // コンストラクタ引数を直接転送
}
}
// リソースの解放
void cleanup() {
large_object = std::nullopt; // 明示的な解放
}
};
// メモリ最適化のためのテクニック
template<typename T>
class OptimizedContainer {
private:
std::vector<std::optional<T>> items;
public:
// 要素の効率的な追加
void addItem(T item) {
// 未使用のスロットを探す
auto it = std::find_if(items.begin(), items.end(),
[](const auto& opt) { return !opt.has_value(); });
if (it != items.end()) {
*it = std::move(item); // 既存のスロットを再利用
} else {
items.emplace_back(std::move(item)); // 新しいスロットを追加
}
}
// 要素の効率的な削除
void removeItem(size_t index) {
if (index < items.size()) {
items[index] = std::nullopt; // メモリを保持したまま要素を無効化
}
}
};
最適化のポイント:
- メモリレイアウトの最適化
// 悪い例:不必要なメモリ使用
struct BadLayout {
std::optional<char> small_value; // パディングが発生
std::optional<double> large_value;
};
// 良い例:メンバ変数の適切な配置
struct GoodLayout {
std::optional<double> large_value; // 8バイトアライメント
std::optional<char> small_value; // パディングを最小化
};
- キャパシティの効率的な管理
// 予約による再アロケーションの削減
std::vector<std::optional<int>> vec;
vec.reserve(expected_size); // メモリの事前確保
// バッチ処理による効率化
void batchProcess(std::vector<std::optional<int>>& vec) {
vec.clear(); // キャパシティを保持したままクリア
vec.shrink_to_fit(); // 必要に応じてメモリを解放
}
- ムーブセマンティクスの活用
std::optional<std::vector<int>> opt; // 効率的な代入 opt = std::vector<int>(1000); // 一時オブジェクトの直接ムーブ // emplace構築 opt.emplace(1000); // 引数を直接転送して構築
これらの最適化テクニックを適切に組み合わせることで、std::optionalを使用したコードのパフォーマンスとメモリ効率を最大限に高めることができます。次のセクションでは、これらの知識を活かした実装例と注意点について見ていきましょう。
std::optionalを使用した実装例と注意点
データベース操作での活用事例
データベース操作において、std::optionalを効果的に活用する実装例を見ていきましょう:
#include <optional>
#include <string>
#include <unordered_map>
// データベース操作を模したクラス
class Database {
private:
struct Record {
int id;
std::string name;
std::optional<std::string> email; // オプショナルなフィールド
std::optional<std::string> phone; // オプショナルなフィールド
};
std::unordered_map<int, Record> records;
public:
// レコードの追加(部分的な情報のみの場合も対応)
void addRecord(int id, const std::string& name,
std::optional<std::string> email = std::nullopt,
std::optional<std::string> phone = std::nullopt) {
records[id] = Record{id, name, email, phone};
}
// 特定フィールドの更新(変更がない場合はnullopt)
bool updateRecord(int id,
const std::optional<std::string>& name = std::nullopt,
const std::optional<std::string>& email = std::nullopt,
const std::optional<std::string>& phone = std::nullopt) {
auto it = records.find(id);
if (it == records.end()) return false;
if (name) it->second.name = *name;
if (email) it->second.email = *email;
if (phone) it->second.phone = *phone;
return true;
}
// レコードの取得(存在しない場合はnullopt)
std::optional<Record> getRecord(int id) const {
auto it = records.find(id);
return it != records.end() ?
std::optional<Record>(it->second) : std::nullopt;
}
};
設定値の管理における実装パターン
設定管理システムでのstd::optionalの活用例:
class Configuration {
private:
struct Settings {
std::optional<int> port;
std::optional<std::string> host;
std::optional<bool> useSSL;
std::optional<std::string> certificatePath;
};
Settings settings;
public:
// ビルダーパターンを活用した設定
class Builder {
Settings settings;
public:
Builder& setPort(int p) {
settings.port = p;
return *this;
}
Builder& setHost(const std::string& h) {
settings.host = h;
return *this;
}
Builder& setSSL(bool ssl) {
settings.useSSL = ssl;
return *this;
}
Builder& setCertificatePath(const std::string& path) {
settings.certificatePath = path;
return *this;
}
Configuration build() {
// SSL使用時は証明書パスが必須
if (settings.useSSL && settings.useSSL.value() &&
!settings.certificatePath) {
throw std::runtime_error("SSL requires certificate path");
}
return Configuration(settings);
}
};
static Builder builder() { return Builder(); }
// 設定値の取得と検証
std::optional<int> getPort() const { return settings.port; }
bool isValid() const {
return settings.host && settings.port &&
(!settings.useSSL.value_or(false) || settings.certificatePath);
}
private:
Configuration(const Settings& s) : settings(s) {}
};
共通の落とし穴と対処方法
std::optional使用時の一般的な問題とその解決策:
class OptionalPitfalls {
public:
// 問題1: 不必要なチェックの重複
void badExample1(const std::optional<std::string>& opt) {
// 悪い例:二重チェック
if (opt.has_value()) {
if (opt.value().empty()) { // 既にチェック済み
// 処理
}
}
// 良い例:一度のチェックで処理
if (opt && !opt->empty()) {
// 処理
}
}
// 問題2: 例外安全性の考慮不足
std::string badExample2(const std::optional<std::string>& opt) {
// 悪い例:例外が発生する可能性
return opt.value(); // 値がない場合に例外
// 良い例:安全なアクセス
return opt.value_or("default");
}
// 問題3: 不適切な比較
bool badExample3(const std::optional<int>& a,
const std::optional<int>& b) {
// 悪い例:nulloptの考慮が不足
return a.value_or(0) < b.value_or(0);
// 良い例:完全な比較
if (!a && !b) return false; // 両方空
if (!a) return true; // aが空
if (!b) return false; // bが空
return *a < *b; // 値の比較
}
// 問題4: ムーブセマンティクスの誤用
void badExample4() {
std::optional<std::vector<int>> opt;
// 悪い例:不必要なコピー
opt = std::vector<int>(100);
// 良い例:直接構築
opt.emplace(100);
}
};
// ベストプラクティスの例
class OptionalBestPractices {
public:
// パターン1: 早期リターン
void processData(const std::optional<std::string>& data) {
if (!data) return; // 早期リターン
// データが存在する場合の処理
processValidData(*data);
}
// パターン2: 型変換の安全な処理
template<typename T, typename U>
std::optional<T> safeCast(const std::optional<U>& source) {
if (!source) return std::nullopt;
if constexpr (std::is_convertible_v<U, T>) {
return static_cast<T>(*source);
}
return std::nullopt;
}
// パターン3: 条件付き更新
template<typename T>
void updateIfValid(std::optional<T>& target,
const std::optional<T>& source) {
if (source && isValid(*source)) {
target = source;
}
}
private:
template<typename T>
bool isValid(const T& value) {
// 値の検証ロジック
return true; // 実際の検証ロジックを実装
}
void processValidData(const std::string& data) {
// 実際の処理ロジック
}
};
これらの実装例と注意点を理解することで、std::optionalをより効果的に活用できます。次のセクションでは、他のエラー処理手法との使い分けについて見ていきましょう。
std::optionalと他のエラー処理手法の使い分け
例外処理との適切な組み合わせ方
std::optionalと例外処理を効果的に組み合わせることで、より堅牢なエラーハンドリングが実現できます:
#include <optional>
#include <stdexcept>
#include <string>
#include <vector>
class ErrorHandlingExample {
public:
// 例外とoptionalの組み合わせ
class DatabaseError : public std::runtime_error {
using std::runtime_error::runtime_error;
};
struct UserData {
std::string name;
int age;
};
// 回復可能な状況にはoptionalを使用
std::optional<UserData> findUser(const std::string& username) {
try {
if (!isConnected()) {
throw DatabaseError("Database connection lost");
}
// ユーザーが見つからない場合はnullopt
if (!userExists(username)) {
return std::nullopt;
}
// ユーザーデータの取得
return UserData{username, 25}; // 簡略化
} catch (const DatabaseError& e) {
// 深刻なエラーは例外として伝播
throw;
} catch (...) {
// その他の予期しないエラーはログ記録後にnullopt
logError("Unexpected error in findUser");
return std::nullopt;
}
}
// エラー処理の階層化
class UserManager {
public:
// 高レベルの操作では例外を使用
void createUser(const std::string& username, int age) {
if (!validateUsername(username)) {
throw std::invalid_argument("Invalid username");
}
auto result = tryCreateUser(username, age);
if (!result) {
throw DatabaseError("Failed to create user");
}
}
private:
// 内部実装では可能な限りoptionalを使用
std::optional<bool> tryCreateUser(const std::string& username, int age) {
if (userExists(username)) {
return std::nullopt;
}
// ユーザー作成ロジック(簡略化)
return true;
}
bool validateUsername(const std::string& username) {
return !username.empty() && username.length() <= 50;
}
bool userExists(const std::string& username) {
return false; // 簡略化
}
};
private:
bool isConnected() { return true; } // 簡略化
bool userExists(const std::string& username) { return false; } // 簡略化
void logError(const std::string& message) { /* ログ記録処理 */ }
};
std::variant、std::anyとの使い分けガイド
各型の特徴と適切な使用場面:
#include <any>
#include <variant>
class TypeSelectionGuide {
public:
// std::optionalの使用例:値の有無を表現
std::optional<int> parseInteger(const std::string& str) {
try {
return std::stoi(str);
} catch (...) {
return std::nullopt;
}
}
// std::variantの使用例:複数の型の可能性を表現
using NumberVariant = std::variant<int, double, std::string>;
NumberVariant parseNumber(const std::string& str) {
try {
return std::stoi(str);
} catch (...) {
try {
return std::stod(str);
} catch (...) {
return str; // 数値として解釈できない場合は文字列として扱う
}
}
}
// std::anyの使用例:型が動的に決定される場合
std::any parseValue(const std::string& str, const std::string& type) {
if (type == "int") {
return std::stoi(str);
} else if (type == "double") {
return std::stod(str);
} else if (type == "bool") {
return str == "true";
}
return str;
}
};
// 実践的な使い分けの例
class DataProcessor {
public:
// optionalとvariantの組み合わせ
struct ProcessResult {
std::optional<std::variant<int, std::string>> data;
std::optional<std::string> error;
};
ProcessResult processData(const std::string& input) {
if (input.empty()) {
return {std::nullopt, "Empty input"};
}
try {
return {std::variant<int, std::string>(std::stoi(input)), std::nullopt};
} catch (...) {
return {std::variant<int, std::string>(input), std::nullopt};
}
}
// 各型の特性を活かした使い分け
class ConfigValue {
std::variant<int, double, std::string> value;
std::optional<std::string> description; // オプショナルな説明
std::any metadata; // 任意の追加データ
public:
template<typename T>
void setValue(T&& val) {
value = std::forward<T>(val);
}
void setDescription(const std::string& desc) {
description = desc;
}
template<typename T>
void setMetadata(T&& data) {
metadata = std::forward<T>(data);
}
};
};
使い分けの基準:
- std::optionalを使用する場合
- 値が存在しない可能性がある場合
- エラー状態が「値の不在」で表現できる場合
- 単一の型に対する操作
- std::variantを使用する場合
- 複数の型の可能性が事前に分かっている場合
- 型安全な多態性が必要な場合
- パターンマッチング的な処理が必要な場合
- std::anyを使用する場合
- 型が完全に動的に決定される場合
- プラグイン機構など、型の制約を設けられない場合
- パフォーマンスよりも柔軟性が重要な場合
実装時の注意点:
class ErrorHandlingGuidelines {
public:
// optionalとvariantの適切な組み合わせ
template<typename T>
class Result {
std::variant<T, std::string> value_or_error;
bool has_value;
public:
static Result<T> success(T value) {
return Result(std::move(value));
}
static Result<T> error(std::string error) {
return Result(std::move(error));
}
bool isSuccess() const { return has_value; }
const T& getValue() const {
return std::get<T>(value_or_error);
}
const std::string& getError() const {
return std::get<std::string>(value_or_error);
}
private:
Result(T value)
: value_or_error(std::move(value)), has_value(true) {}
Result(std::string error)
: value_or_error(std::move(error)), has_value(false) {}
};
// 実践的な使用例
Result<int> processValue(const std::string& input) {
auto parsed = parseInteger(input);
if (!parsed) {
return Result<int>::error("Failed to parse integer");
}
if (*parsed < 0) {
return Result<int>::error("Negative values not allowed");
}
return Result<int>::success(*parsed);
}
private:
std::optional<int> parseInteger(const std::string& str) {
try {
return std::stoi(str);
} catch (...) {
return std::nullopt;
}
}
};
以上のように、各エラー処理手法には適切な使用場面があり、それらを組み合わせることで、より堅牢なエラーハンドリングを実現できます。