On Tuesday, 17 June 2014 at 20:13:39 UTC, Luís Marques wrote:
Consider this:
struct S
{
void opDispatch(string name, T)(T value)
{
alias AllowedTypes = SomeComplexTemplate!S;
//static assert(is(T : AllowedTypes, "wrong type"));
static if(!is(T : AllowedTypes))
{
pragma(msg, "wrong type");
static assert(0);
}
}
}
If the static assert fails, then the opDispatch instantiation
fails, and so the relevant member is declared not found.
Because of SFINAE, I assume (no?), the user/developer will
never see the "wrong type" error message. So, instead, you have
to use a pragma(msg), saying something like "hey, ignore the
message bellow saying 'name' is not found; it's totally there,
the problem is that you tried to assign a value of an invalid
type".
Is there a way to improve this situation?
(BTW, the documentation for opDispatch is a bit thin on the
details.)
No SFINAE please. That is an idiotic C++ idiom. use template
constraints if you want to disable this template for some
parameter sets. static assert is to statically assert. In this
case it fails. As it should !