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

Conditional compilation based on templates

21 views
Skip to first unread message

MrSpoo...@636umvily2vqnpy.com

unread,
Mar 20, 2021, 6:48:10 AM3/20/21
to
Is there any way of munging this or if not does anyone know if it might be
coming in the forthcoming standard?

Eg I'd like code similar to the following to compile:

if (typeid(T) != typeid(chrono::steady_clock))
{
time_t tend = T::to_time_t(end);
cout << "Finished at: " << ctime(&tend);
}

Sure, I could just specialise a function but the above would be a lot more
convenient.

Alf P. Steinbach

unread,
Mar 20, 2021, 11:25:19 AM3/20/21
to
If this is in template code then you can just check the type at compile
time, like

if constexpr( not is_same_v<T, chrono::steady_clock> ) {

Possibly you may have to remove reference and cv-qualification of T.

Ref. <url: https://en.cppreference.com/w/cpp/types/is_same>

---

Not what you're asking, but the code essentially says that only if type
T is not a steady_clock do you want to output the end time, and in that
case you're sure that type T has a to_time_t static member function, and
that's all quite baffling to me.

I would think about ways to reorganize that so that it doesn't depend so
much on the details of the (unspecified) context.

Because: as general habit that makes it easier to change things in code
without having to worry about that a piece of it here or there might
stop working because it depended on a contextual detail.


- Alf

MrSpoo...@s5p.net

unread,
Mar 20, 2021, 11:38:20 AM3/20/21
to
On Sat, 20 Mar 2021 16:25:07 +0100
"Alf P. Steinbach" <alf.p.stein...@gmail.com> wrote:
>On 20.03.2021 11:47, MrSpoo...@636umvily2vqnpy.com wrote:
>> Is there any way of munging this or if not does anyone know if it might be
>> coming in the forthcoming standard?
>>
>> Eg I'd like code similar to the following to compile:
>>
>> if (typeid(T) != typeid(chrono::steady_clock))
>> {
>> time_t tend = T::to_time_t(end);
>> cout << "Finished at: " << ctime(&tend);
>> }
>>
>> Sure, I could just specialise a function but the above would be a lot more
>> convenient.
>
>If this is in template code then you can just check the type at compile
>time, like
>
> if constexpr( not is_same_v<T, chrono::steady_clock> ) {
>
>Possibly you may have to remove reference and cv-qualification of T.

Thanks, worked.


0 new messages