[cmake-developers] if (FOO == BAR) ...

David Cole dlrdave at aol.com
Thu Mar 21 16:55:02 EDT 2013


I almost always do one of these for string compare to a CMake variable
value:

    if("${var}" STREQUAL "some string constant")
    if("${var}" STREQUAL "${some_other_variable}")

However, this is only because I am almost always certain that ${var}
does not evaluate to the name of yet another CMake variable.
If it did, I would get unintended results.

So I wouldn't say it's necessarily a best-practice. It's "close enough"
for many lines of code I've written, but a monkey-wrench could easily
be thrown at it.

Does forcing the if(VARIABLE usage rather than the if("string" usage
make things work all the time? (Now I've thought about it too hard,
and I can't remember if this works all the time or not...)

    set(x "${var}")
    if(x STREQUAL "some string constant")


D


-----Original Message-----
From: Clinton Stimpson <clinton at elemtech.com>
To: cmake-developers <cmake-developers at cmake.org>
Sent: Thu, Mar 21, 2013 3:40 pm
Subject: Re: [cmake-developers] if (FOO == BAR) ...



Is there a best practices using existing functionality to always avoid
automatic dereference when comparing strings?

Clint

On Thursday, March 21, 2013 02:56:51 PM David Cole wrote:
> Unfortunately, this entire discussion nicely demonstrates and
> reinforces my belief that we ought not to do this == thing...
>
> If Alex, Brad, Matthew and I can't understand each other's meanings
> within the context of this discussion, what chance does a poor user
> reading through the CMake IF documentation have of getting it right?
>
> Adding a new better way to do something without eliminating the old
> not-so-good way of doing the same thing is not necessarily a good
> thing. Multiple ways to do things just make people shake their heads
> and ask "Why?"
>
> I am still voting no on this one.
>
>
> David C.
>
>
> -----Original Message-----
> From: Alexander Neundorf <neundorf at kde.org>
> To: cmake-developers <cmake-developers at cmake.org>
> Sent: Thu, Mar 21, 2013 2:43 pm
> Subject: Re: [cmake-developers] if (FOO == BAR) ...
>
> On Thursday 21 March 2013, Matthew Woehlke wrote:
> > On 2013-03-20 17:42, Alexander Neundorf wrote:
> > > On Wednesday 20 March 2013, Matthew Woehlke wrote:
> > >> On 2013-03-20 17:10, David Cole wrote:
> > >>> Are you proposing that == behaves as STREQUAL, or as EQUAL?
> > >>
> > >> What's the difference?
> > >>
> > >> Okay, for <, >, there is an obvious answer, but for ==, I am
>
> trying and
>
> > >> failing to think of a situation where treating the arguments as
>
> numbers
>
> > >> would give a different result vs. treating them as strings.
> > >
> > > E.g. "0" vs. "0.0"
> >
> > Is "0.0" a floating-point number or a version string? In the context
>
> of
>
> > CMake, I would have rather expected it to be the latter. (Does CMake
> > even understand floating point?) If '==' assumes numbers, how do I
>
> tell
>
> > it I really meant a version string?
>
> EQUAL tries to convert to double:
>
>   if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
>         (*(argP1) == "LESS" || *(argP1) == "GREATER" ||
>          *(argP1) == "EQUAL"))
>         {
>         def = cmIfCommand::GetVariableOrString(arg->c_str(),
makefile);
>          def2 = cmIfCommand::GetVariableOrString((argP2)->c_str(),
> makefile);
>         double lhs;
>         double rhs;
>         bool result;
>         if(sscanf(def, "%lg", &lhs) != 1 ||
>            sscanf(def2, "%lg", &rhs) != 1)
>
>
> My "==" implementation simply compares the strings on both sides in
the
> most
> simple and straightforward way.
>
>
> Alex
> --
>
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/cgi-bin/mailman/listinfo/cmake-developers
>
>
> --
>
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/cgi-bin/mailman/listinfo/cmake-developers
--
Clinton Stimpson
Elemental Technologies, Inc
Computational Simulation Software, LLC
www.csimsoft.com
--

Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the CMake FAQ at:
http://www.cmake.org/Wiki/CMake_FAQ

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/cgi-bin/mailman/listinfo/cmake-developers










More information about the cmake-developers mailing list