Boost logo

Boost :

Subject: Re: [boost] How can I detect if a template static member function exists?
From: Edward Diener (eldiener_at_[hidden])
Date: 2016-10-16 09:10:31


On 10/16/2016 7:16 AM, Vinnie Falco wrote:
> I'd like to write a trait:
>
> template<class T>
> struct has_content_length :
> std::integral_constant<bool, ...>{};
>
> which detects whether or not a template static member function is
> present in T. Here's a typical T:
>
> struct X
> {
> template<bool isRequest, class Body, class Headers>
> static
> std::uint64_t
> content_length(
> message<isRequest, Body, Headers> const& m);
> };
>
> How can I detect if X::content_length(m) is callable for any m of type
> message template?

It is not possible AFAIK. The tti library can detect whether a class
template is a member of a type but it cannot detect whether a function
template is a member of a type. The reason for this, within tti, is that
class templates and function templates are not treated orthogonally in
C++; a class template can be a template parameter but a function
template cannot be a template parameter. If a function template could be
a template parameter then tti could detect a function template as a
member of a type, whether that function template were a static or a
non-static member.

I have felt that the lack of orthogonality in C++, where a class
template can be a template parameter but a function template cannot be a
template parameter, is a design weakness of C++. But I would have to
convince the C++ standards committee of the practicality of allowing a
function template to be a template parameter to possibly change this. I
doubt whether saying that the change would allow the Boost tti library
to detect function templates in the same way it can detect class
templates, would be a good enough practical reason <g>.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk