std::visit()
cppreference のページ (
https://en.cppreference.com/w/cpp/utility/variant/visit ) を見ると、理解できないコードに遭遇しました...
短縮版は次のとおりです。
#include <iomanip>
#include <iostream>
#include <string>
#include <type_traits>
#include <variant>
#include <vector>
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...)->overloaded<Ts...>;
int main() {
std::vector<std::variant<int,long,double,std::string>> vec = { 10, 15l, 1.5, "hello" };
for (auto& v : vec) {
std::visit(overloaded{
[](auto arg) { std::cout << arg << ' '; },
[](double arg) { std::cout << std::fixed << arg << ' '; },
[](const std::string& arg) { std::cout << std::quoted(arg) << ' '; },
}, v);
}
}
overloaded
のすぐ上にある を宣言している 2 行は何int main()
を意味していますか?
説明してくれてありがとう!
2019 追加
以下の 2 人の紳士が詳細な説明を提供した後 (どうもありがとうございました!)、非常に優れた本C++17 in Detail - Learn the Exciting Features of The New C++ Standard!で同じコードを偶然見つけました。Bartłomiej Filipekによる。そのようなよく書かれた本!