Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Could we use T:X to abbreviate "typename T::X"? (and get cleaner code)

248 views
Skip to first unread message

Walter Mascarenhas

unread,
Jan 22, 2013, 11:54:51 AM1/22/13
to

When I use constructs like std::enable_if my code gets polluted with many typenames, as in

typename std::enable_if< std::is_same<typename A::X, typename B::Y>::value,
typename C::Z>::type

Would it be too costly to introduce a symbol, say ':', in the language so
that "T:X" is an abbreviation for "typename T::X"?

This would lead to this cleaner version of the line above:

std::enable_if< std::is_same<A:X, B:Y>::value, C:Z>::type

I also dream of writing this in the even simpler form

std::enable_if< std::is_same<A:X, B:Y>, C:Z> (meaning C:Z when A:X = B:Y)

but I guess this would be too much to ask for.





--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp...@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Daniel Krügler

unread,
Jan 22, 2013, 6:26:32 PM1/22/13
to
Am 22.01.2013 17:54, schrieb Walter Mascarenhas:
>
>
> When I use constructs like std::enable_if my code gets polluted with many typenames, as in
>
> typename std::enable_if< std::is_same<typename A::X, typename B::Y>::value,
> typename C::Z>::type
>
> Would it be too costly to introduce a symbol, say ':', in the language so
> that "T:X" is an abbreviation for "typename T::X"?


An alternative solution without need of any further language
extensions is the usage of a template alias instead. Typical examples
are:

template<bool C, class T = void>
using EnableIf = typename std::enable_if<C, T>::type;

HTH & Greetings from Bremen,

Daniel Krügler

Walter Mascarenhas

unread,
Jan 24, 2013, 6:15:38 PM1/24/13
to
On Tuesday, January 22, 2013 9:26:32 PM UTC-2, Daniel Krügler wrote:
> Am 22.01.2013 17:54, schrieb Walter Mascarenhas:
>
>
> > When I use constructs like std::enable_if my code gets polluted with many typenames, as in
> > typename std::enable_if< std::is_same<typename A::X, typename B::Y>::value,
> > typename C::Z>::type
>

> > Would it be too costly to introduce a symbol, say ':', in the language so
> > that "T:X" is an abbreviation for "typename T::X"?
>
>
> An alternative solution without need of any further language
> extensions is the usage of a template alias instead. Typical examples
> are:
>
> template<bool C, class T = void>
> using EnableIf = typename std::enable_if<C, T>::type;
>
>
> HTH& Greetings from Bremen,
>
>
>
> Daniel Krügler
>


Thanks for the reply. This is indeed a
good alternative for people with good
compilers.

Unfortunately I am stuck with Visual C++
and won't be able to apply this solution
for a while. Of course microsoft is the
one to blame here and people in this
list shouldn't worry about this question.

Greetings from Brazil,

walter.

SG

unread,
Feb 27, 2013, 12:05:37 PM2/27/13
to
On Jan 23, 12:26 am, Daniel Kr�gler wrote:
> Am 22.01.2013 17:54, schrieb Walter Mascarenhas:
> > When I use constructs like std::enable_if my code gets polluted with many typenames,
> > as in
> >
> > typename std::enable_if< std::is_same<typename A::X, typename B::Y>::value,
> > typename C::Z>::type

[snip]

> template<bool C, class T = void>
> using EnableIf = typename std::enable_if<C, T>::type;

Is the right hand side of this template alias considered an immediate
context with respect to SFINAE?

Cheers!
S.

Daniel Krügler

unread,
Feb 27, 2013, 1:52:35 PM2/27/13
to
Am 27.02.2013 18:05, schrieb SG:
>
> On Jan 23, 12:26 am, Daniel Kr�gler wrote:
> [snip]
>
>> template<bool C, class T = void>
>> using EnableIf = typename std::enable_if<C, T>::type;
>
>
> Is the right hand side of this template alias considered an immediate
> context with respect to SFINAE?


Let me start with a general remark in regard to "immediate context"
used in 14.8: The Standard committee had introduced this term knowing
that this is a fuzzy definition. In regard to alias templates the
wording *could* be interpreted as if the "definition of alias would be
a different context. In general, this is not intended, because alias
substitution is supposed to happen very early. I think that this usage
of alias template is safe (and considered to be in the immediate
context) when you compare it with

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1558

The current suggestion is that

a) Substitution happens in the immediate context of the template that
had causes the substitution

b) Access checking (see CWG issue

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1554
) is performed in the context of the definition of alias template.

HTH & Greetings from Bremen,

Daniel Kr�gler
0 new messages