Hi to all,

when trying to compile the code below, VC7.1 does it, even if
GetSomeSpecialWin32TickCount is unknown.
However both GCC and Comeau (in strict mode) give an error.

What about compiling unused parst of template classes? We assumed compilers
NOT to look inside member function bodies (and in fact VC7.1 does not even
detect missing ;)




#include <ctime>


struct win32 {};
struct other {};

typedef other os_type;



template <typename timetraits_t, typename timer_t = typename
timetraits_t::value_type>
class basic_timer
{
timer_t start_;
timer_t stop_;

inline static timer_t now()
{
return timetraits_t::get_time(os_type());
}

public:

typedef timer_t value_type;

basic_timer()
: start_(0)
{
}

value_type start()
{
return start_ = now();
}

value_type stop()
{
return (stop_ = now())-start_;
}

};

//////////////////////////////////////////////////////////////////////////


template <typename platform_t>
struct clock_traits
{
typedef size_t value_type;

template <typename any_t>
inline static value_type get_time(any_t)
{
time_t t;
return std::time(&t);
}

inline static value_type get_time(win32)
{
return GetSomeSpecialWin32TickCount();
}

};



typedef basic_timer< clock_traits< os_type > > timer_chrono;


//////////////////////////////////////////////////////////////////////////


int main()
{
timer_chrono t;
t.start();
t.stop();
return 0;
}

Re: unused template class methods by Mycroft

Mycroft
Fri May 06 07:02:12 CDT 2005

> when trying to compile the code below, VC7.1 does it, even if
> GetSomeSpecialWin32TickCount is unknown.
> However both GCC and Comeau (in strict mode) give an error.
>

small correction: GCC does not give an error, unless the body of the
function contains an unknown TYPE, for example

inline static value_type get_time(win32)
{
return GetTickCount();
}


is fine, but this is not:

inline static value_type get_time(win32)
{
LARGE_INTEGER l;
return GetTickCount();
}



Re: unused template class methods by Carl

Carl
Fri May 06 08:13:51 CDT 2005

Mycroft Holmes wrote:
> Hi to all,
>
> when trying to compile the code below, VC7.1 does it, even if
> GetSomeSpecialWin32TickCount is unknown.
> However both GCC and Comeau (in strict mode) give an error.
>
> What about compiling unused parst of template classes? We assumed
> compilers NOT to look inside member function bodies (and in fact
> VC7.1 does not even detect missing ;)

The c++ standard requires that certain kinds of errors in templates are
diagnosed even if the template is never instantiated. Biascally, the
template needs to be syntactically valid, and non-dependent identifiers
(names of types, functions, etc that don't depend on the template
parameter(s)) must exist. Comeau is probably the only compiler that
actually checks everything that the standard requires, while VC++ is close
to, if not at the other end: the compiler just about ignores the contents
of a template function unless it's instantiated.

-cd



Re: unused template class methods by Tom

Tom
Fri May 06 10:04:29 CDT 2005

Carl Daniel [VC++ MVP] wrote:
> Mycroft Holmes wrote:
>
>>Hi to all,
>>
>>when trying to compile the code below, VC7.1 does it, even if
>>GetSomeSpecialWin32TickCount is unknown.
>>However both GCC and Comeau (in strict mode) give an error.
>>
>>What about compiling unused parst of template classes? We assumed
>>compilers NOT to look inside member function bodies (and in fact
>>VC7.1 does not even detect missing ;)
>
>
> The c++ standard requires that certain kinds of errors in templates are
> diagnosed even if the template is never instantiated.

I don't think it requires diagnosis in this case, only allows it (14.6/7
- "... no diagnostic required").

Biascally, the
> template needs to be syntactically valid, and non-dependent identifiers
> (names of types, functions, etc that don't depend on the template
> parameter(s)) must exist. Comeau is probably the only compiler that
> actually checks everything that the standard requires, while VC++ is close
> to, if not at the other end: the compiler just about ignores the contents
> of a template function unless it's instantiated.

I think GCC 3.4 and GCC 4.0 both do the checking too, unless you pass
-fpermissive. But the checking is an optional feature for a standards
compliant compiler.

Tom

Re: unused template class methods by Carl

Carl
Fri May 06 10:35:53 CDT 2005

Tom Widmer wrote:
> Carl Daniel [VC++ MVP] wrote:
>> Mycroft Holmes wrote:
>>
>>> Hi to all,
>>>
>>> when trying to compile the code below, VC7.1 does it, even if
>>> GetSomeSpecialWin32TickCount is unknown.
>>> However both GCC and Comeau (in strict mode) give an error.
>>>
>>> What about compiling unused parst of template classes? We assumed
>>> compilers NOT to look inside member function bodies (and in fact
>>> VC7.1 does not even detect missing ;)
>>
>>
>> The c++ standard requires that certain kinds of errors in templates
>> are diagnosed even if the template is never instantiated.
>
> I don't think it requires diagnosis in this case, only allows it
> (14.6/7 - "... no diagnostic required").
>

Right you are, so it's just a QoI issue.

-cd



Re: unused template class methods by Mycroft

Mycroft
Mon May 09 02:33:25 CDT 2005


>>> The c++ standard requires that certain kinds of errors in templates
>>> are diagnosed even if the template is never instantiated.
>>
>> I don't think it requires diagnosis in this case, only allows it
>> (14.6/7 - "... no diagnostic required").
>>
>
> Right you are, so it's just a QoI issue.
>
> -cd
>
>

thnaks for pointing out the paragraph in the standard.
does this mean that it's unpredictable if the original code will be compiled
or not on a standard compiler?



Re: unused template class methods by Tom

Tom
Mon May 09 04:37:30 CDT 2005

Mycroft Holmes wrote:
>>>>The c++ standard requires that certain kinds of errors in templates
>>>>are diagnosed even if the template is never instantiated.
>>>
>>>I don't think it requires diagnosis in this case, only allows it
>>>(14.6/7 - "... no diagnostic required").
>>>
>>
>>Right you are, so it's just a QoI issue.
>>
>>-cd
>>
>>
>
>
> thnaks for pointing out the paragraph in the standard.
> does this mean that it's unpredictable if the original code will be compiled
> or not on a standard compiler?

Yes, it is unpredictable, but the code is ill-formed in any case, so you
can't rely on anything from the resultant executable according to the
standard (though, in practice, it will work "fine").

Tom

Re: unused template class methods by Mycroft

Mycroft
Mon May 09 06:07:56 CDT 2005

>> does this mean that it's unpredictable if the original code will be
>> compiled or not on a standard compiler?
>
> Yes, it is unpredictable, but the code is ill-formed in any case, so you
> can't rely on anything from the resultant executable according to the
> standard (though, in practice, it will work "fine").
>

thanks!