template<typename CantBeWeirdType>
class complicated
{
static_assert(
! is_weird<CantBeWeirdType>::value,
"Can't create a complicated<%[0]>, %[0] is weird!\n"
"Also %0 is %1 bytes in size, which is probably bad!\n"
"The alignment of %[0] is 0x%xp[2], which is not supported!",
// Argument 0: Plain string literal (unqualified name)
nameof(CantBeWeirdType),
// Argument 1: Lossless string literal in base-10 by default
sizeof(CantBeWeirdType)
// Argument 2: Lossless string literal in base-16 (%x)
// E.G: ABCD (no 0x)
alignof(CantBeWeirdType)
);
template<typename ArgumentType>
void f(ArgumentType &&x)
{
static_assert(
! is_weird<decay<ArgumentType>>::value,
"Argument %[0] is of a weird type!\n",
// Unqualified name, (reduces to "x", but using symbols helps refactoring tools...)
nameof(x)
);
}
};
Error <line><file>: Can't create a complicated<weird_thing>, weird_thing is weird!
Also, weird_thing is 42 bytes in size, which is probably bad!
The alignment of weird_thing is 0x0000DC81, which is not acceptable!
//nameof, qnameof, and dnameof
namespace space_of_names{
struct type{};
}
// Expands to string literal "type"
auto name_str = nameof(space_of_names::type);
// Expands to string literal "space_of_names::type"
auto qname_str = qnameof(space_of_names::type);
// Expands to string literal ".?QUtype@space_of_names@@" (through MSVC)
auto dname_str = dnameof(space_of_names::type);
// And similarly for variable names.
%<style-opt><width-opt><[base-10 arg index]>
<style> is one of:
'' <- Decimal (nil)
'x' <- Hexadecimal
'o' <- Octal
'b' <- Binary
<width> can be a base-10 number of spaces to pad up to
or 'p' for 'width of pointer'. Integers are 0-padded,
strings are space-padded.
static_assert(sizeof(int) == 4);
template<typename T, T v>
struct Expected : std::integral_constant<T, v> {};
template<typename T, T v>
struct Actual : std::integral_constant<T, v> {};
template<typename E, typename A>
struct AssertEqual;
template<template<typename LL, LL> class L, template<typename RR, RR> class R, typename T, T v>
struct AssertEqual<L<T, v>, R<T, v>> : std::true_type {};
static_assert(AssertEqual<Expected<size_t, 4>, Actual<size_t, sizeof(int)>>(), "UGH");
*snip*
We should be able to do better, not that I know what such a proposal would look like...
class coconuts{};
coconuts bunch{};
static const size_t count = 55;
// Inherits the width of the format string.
//
// vnameof() applies only to variables:
// Probably has other options, just makes a lossless base-10 string literal by default.
//
const auto *str = static_format("I've got a lovely %0 of %2 %1", nameof(coconuts), nameof(bunch), vnameof(count));
On 2015–02–20, at 1:39 AM, Ville Voutilainen <ville.vo...@gmail.com> wrote:Seems like SG7 material, a combination of reflection and compile-time strings.
Somewhat hard, somewhat niche, not useful enough imho to put all of that
functionality into static assert. Most of the necessary pieces might
become available
in the future and then you could combine them generally.
constexpr void static_assert(constexpr bool bool_constexpr, constexpr const char *constexpr pmessage = nullptr) ;constexpr void static_assert(constexpr bool bool_constexpr, constexpr char *constexpr pmessage = nullptr) ;int var;
int *constexpr ptr_var = &var; //here 'ptr_var' is an address which can be represented by the compiler as '&var'
int *ptr_var_n = new int; //here 'ptr_var_n' address can't be represented by the compiler as it's determined during run-time by an external non-constexpr function callconstexpr void static_assert(constexpr bool bool_constexpr, constexpr const char *constexpr pstrformat, ...) ;
On 2015–02–22, at 1:05 AM, Ville Voutilainen <ville.vo...@gmail.com> wrote:On 21 February 2015 at 17:41, David Krauss <pot...@gmail.com> wrote:We currently have all that’s needed for the 99% of users who won’t be meticulously formatting strings, which is simply to specify a pretty-printer.
Then by all means let's specify a compile-time printer. Specifying a
static_assert-specific pretty
printer doesn't make much sense.
While we are specifying such a
compile-time printer,
we will probably want compile-time string-processing in general, since
it's useful for far
more than just printing diagnostics.