std::meta::exception
From cppreference.com
| Defined in header <meta>
|
||
class exception : public std::exception
|
(since C++26) | |
Defines a type of object to be thrown on failure by reflection functions.
Objects of type std::meta::exception can only be created and accessed in constant evaluation.
Member functions
(constructor) |
constructs a meta::exception object (public member function) |
what |
returns the explanatory string (public member function) |
u8what |
returns the explanatory string as a std::u8string_view (public member function) |
from |
returns the associated std::meta::info object (public member function) |
where |
returns the associated std::source_location (public member function) |
std::meta::exception::exception
consteval exception( std::u8string_view u8what_arg,
std::meta::info from,
std::source_location where =
std::source_location::current() ) noexcept;
|
(1) | (since C++26) |
consteval exception( std::string_view what_arg,
std::meta::info from,
std::source_location where =
std::source_location::current() ) noexcept;
|
(2) | (since C++26) |
consteval exception( const exception& ) = default;
|
(3) | (since C++26) |
consteval exception( exception&& ) = default;
|
(4) | (since C++26) |
Constructs a new std::meta::info object. Initializes the stored values with the corresponding parameters.
1) If
u8what_arg can be represented in the ordinary literal encoding, the explanatory string returned by what() is u8what_arg transcoded from UTF-8 to the ordinary literal encoding; otherwise, a call to what() is not a constant expression.2)
what_arg must be representable in UTF-8.3) Copy constructor is defaulted.
4) Move constructor is defaulted.
std::meta::exception::what
constexpr const char* what() const noexcept override;
|
(since C++26) | |
Returns the explanatory string passed at the time of construction, possibly transcoded from UTF-8 to the ordinary literal encoding.
std::meta::exception::u8what
consteval std::u8string_view u8what() const noexcept;
|
(since C++26) | |
Returns the explanatory string passed at the time of construction, possibly transcoded from the ordinary literal encoding to UTF-8.
std::meta::exception::from
consteval std::meta::info from() const noexcept;
|
(since C++26) | |
Returns the std::meta::info value passed at the time of construction.
std::meta::exception::where
consteval std::source_location where() const noexcept;
|
(since C++26) | |
Returns the std::source_location value passed at the time of construction.
Example
Run this code
#include <initializer_list>
#include <meta>
#include <print>
#include <source_location>
#include <string_view>
// Let our network protocol structs are forbidden to have data members named “secret”
struct GoodMessage { int id, payload; };
struct BadMessage
{
int id;
int secret; // This will trigger the std::meta::exception
};
consteval void validate_protocol_type(std::meta::info type_reflection)
{
constexpr auto ctx{std::meta::access_context::unchecked()};
for (auto member : std::meta::nonstatic_data_members_of(type_reflection, ctx))
if (std::meta::identifier_of(member) == "secret")
throw std::meta::exception("Forbidden “secret” field detected.",
member, std::source_location::current());
}
consteval bool validate_metadata(const std::initializer_list<std::meta::info> messages)
{
for (auto message : messages)
try
{
validate_protocol_type(message);
}
catch (const std::meta::exception& ex)
{
#ifdef __cpp_lib_compile_time_messages
std::constexpr_print(
"Message: {}\n"
"Token: {}\n"
"Location: {}:{}\n"
, ex.what()
, std::meta::display_string_of(ex.from())
, ex.where().function_name()
, ex.where().line()
);
#endif
return false;
}
return true;
}
constinit auto _{validate_metadata({^^GoodMessage, ^^BadMessage})};
int main() {}
Possible output:
Message: Forbidden “secret” field detected.
Token: int secret
Location: consteval void validate_protocol_type(std::meta::info):23