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

David Cole dlrdave at aol.com
Sat Mar 23 14:25:52 EDT 2013


Wow, could be a variable on the right, too. So my previous example was 
still not 100% safe.

Maybe this construct is 100% safe:

    set(x "${some_var_that_may_eval_to_another_var_name}")
    set(y "some string constant that may also accidentally be a var")
    if(x STREQUAL y)

You can see why the "x${var}" STREQUAL "xSomeString" approach is 
appealing after a discussion like this. Probably safe enough when 
variable names might accidentally be involved in a "strings intended" 
STREQUAL comparison.


D


-----Original Message-----
From: Alexander Neundorf <neundorf at kde.org>
To: cmake-developers <cmake-developers at cmake.org>
Sent: Sat, Mar 23, 2013 10:21 am
Subject: Re: [cmake-developers] if (FOO == BAR) ...


On Thursday 21 March 2013, Matthew Woehlke wrote:
> On 2013-03-21 16:55, David Cole wrote:
> > 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")
>
> I think this would be the only way to be 100% safe.

I think the right side also needs to be a variable, there might be a 
variable
named "some string constant" (ok, usually there isn't).

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

  



More information about the cmake-developers mailing list