Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #13992 > unrolled thread

Compile-time introspection of free-floating functions, does this work?

Started byJuha Nieminen <nospam@thanks.invalid>
First post2012-02-13 19:11 +0000
Last post2012-02-14 00:15 +0000
Articles 5 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  Compile-time introspection of free-floating functions, does this work? Juha Nieminen <nospam@thanks.invalid> - 2012-02-13 19:11 +0000
    Re: Compile-time introspection of free-floating functions, does this work? Victor Bazarov <v.bazarov@comcast.invalid> - 2012-02-13 14:21 -0500
      Re: Compile-time introspection of free-floating functions, does this work? Juha Nieminen <nospam@thanks.invalid> - 2012-02-14 06:38 +0000
      Re: Compile-time introspection of free-floating functions, does this   work? Ian Collins <ian-news@hotmail.com> - 2012-02-14 19:51 +1300
    Re: Compile-time introspection of free-floating functions, does this work? Marc <marc.glisse@gmail.com> - 2012-02-14 00:15 +0000

#13992 — Compile-time introspection of free-floating functions, does this work?

FromJuha Nieminen <nospam@thanks.invalid>
Date2012-02-13 19:11 +0000
SubjectCompile-time introspection of free-floating functions, does this work?
Message-ID<4f39604d$0$4386$7b1e8fa0@news.nbl.fi>
  I think I might have come up with a way to detect at runtime if a
free-floating function has been defined or not, and avoid referencing
it if it isn't. More concretely, in a project of mine I need to know
if std::strtold exists in <cstdlib> or not, and call it only if it does
(and if it doesn't, fall back to calling std::strtod).

  What's the jury's opinion of this? Standard, non-standard, ill-formed,
suspicious? Any suggestions to make it better?

//---------------------------------------------------------------------
#include <iostream>
#include <cstdlib>

namespace std
{
    template<typename T1, typename T2>
    char strtold(T1, T2);
}

namespace
{
    const bool hasStrtold =
        (sizeof(std::strtold("0", (char**)0)) > 1);

    template<bool> struct Func;

    template<> struct Func<false>
    {
        static void func() { std::cout << "No strtold().\n"; }
    };

    template<> struct Func<true>
    {
        static void func()
        {
            std::cout << "Has strtold(). Test: "
                      << std::strtold("12.3", (char**)0) << "\n";
        }
    };

    void func()
    {
        Func<hasStrtold>::func();
    }
}

int main()
{
    func();
}
//---------------------------------------------------------------------

[toc] | [next] | [standalone]


#13994

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2012-02-13 14:21 -0500
Message-ID<jhbns9$emd$1@dont-email.me>
In reply to#13992
On 2/13/2012 2:11 PM, Juha Nieminen wrote:
>    I think I might have come up with a way to detect at runtime if a
> free-floating function has been defined or not, and avoid referencing
> it if it isn't. More concretely, in a project of mine I need to know
> if std::strtold exists in<cstdlib>  or not, and call it only if it does
> (and if it doesn't, fall back to calling std::strtod).
>
>    What's the jury's opinion of this? Standard, non-standard, ill-formed,
> suspicious? Any suggestions to make it better?
>
> //---------------------------------------------------------------------
> #include<iostream>
> #include<cstdlib>
>
> namespace std
> {
>      template<typename T1, typename T2>
>      char strtold(T1, T2);
> }
>
> namespace
> {
>      const bool hasStrtold =
>          (sizeof(std::strtold("0", (char**)0))>  1);
>
>      template<bool>  struct Func;
>
>      template<>  struct Func<false>
>      {
>          static void func() { std::cout<<  "No strtold().\n"; }
>      };
>
>      template<>  struct Func<true>
>      {
>          static void func()
>          {
>              std::cout<<  "Has strtold(). Test:"
>                        <<  std::strtold("12.3", (char**)0)<<  "\n";
>          }
>      };
>
>      void func()
>      {
>          Func<hasStrtold>::func();
>      }
> }
>
> int main()
> {
>      func();
> }
> //---------------------------------------------------------------------

So far the only suspicious part in this code is when you define your own 
'strtold' template in the 'std' namespace - are you allowed to?  I am 
not sure what the correct answer is to that.

Another small issue is that the Standard claims that 'strtold' exists. 
Are you working to identify a non-compliance in a particular compiler? 
I'm asking because a compliant compiler will have 'strtold' defined, I 
think, at least according to 21.7/T78.  Of course, I may have missed 
something there...

V
-- 
I do not respond to top-posted replies, please don't ask

[toc] | [prev] | [next] | [standalone]


#14026

FromJuha Nieminen <nospam@thanks.invalid>
Date2012-02-14 06:38 +0000
Message-ID<4f3a0180$0$3103$7b1e8fa0@news.nbl.fi>
In reply to#13994
Victor Bazarov <v.bazarov@comcast.invalid> wrote:
> Another small issue is that the Standard claims that 'strtold' exists. 

  The C++11 maybe, not the C++98 one. Unfortunately it's a bit too early
to be demanding a C++11-compliant compiler. In a few years maybe.

[toc] | [prev] | [next] | [standalone]


#14029 — Re: Compile-time introspection of free-floating functions, does this work?

FromIan Collins <ian-news@hotmail.com>
Date2012-02-14 19:51 +1300
SubjectRe: Compile-time introspection of free-floating functions, does this work?
Message-ID<9puej0Frf4U1@mid.individual.net>
In reply to#13994
On 02/14/12 08:21 AM, Victor Bazarov wrote:
>
> So far the only suspicious part in this code is when you define your own
> 'strtold' template in the 'std' namespace - are you allowed to?  I am
> not sure what the correct answer is to that.

I'm pretty sure any function beginning with str is reserved, at least in 
the C standard.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#14005

FromMarc <marc.glisse@gmail.com>
Date2012-02-14 00:15 +0000
Message-ID<jhc93d$hf7$1@news-rocq.inria.fr>
In reply to#13992
Juha Nieminen  wrote:

>   I think I might have come up with a way to detect at runtime if a
> free-floating function has been defined or not, and avoid referencing
> it if it isn't. More concretely, in a project of mine I need to know
> if std::strtold exists in <cstdlib> or not, and call it only if it does
> (and if it doesn't, fall back to calling std::strtod).

Just as something to compare against, I wrote something for a similar
purpose here:
http://gcc.gnu.org/ml/gcc/2012-01/msg00048.html

with the added complication that the function to test is in the global
namespace and that I wanted to define the function (hence a likely odr
violation), but the idea should be there.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web