[cmake-developers] Please review CXXFeatures.cmake

Rolf Eike Beer eike at sf-mail.de
Fri Sep 20 15:42:29 EDT 2013


Am Dienstag, 13. August 2013, 00:13:19 schrieb Stephen Kelly:
> Rolf Eike Beer wrote:
> >> 5)
> >>
> >> This is not nice API:
> >>#if defined (CXXFEATURES_NULLPTR_FOUND)
> >>void *nix = nullptr;
> >>#else /* CXXFEATURES_NULLPTR_FOUND */
> >>void *nix = 0;
> >>#endif /* CXXFEATURES_NULLPTR_FOUND */
> >>
> >> Much better would be:
> >>void *nix = CXXFEATURES_NULLPTR;
> >>
> >> where -DCXXFEATURES_NULLPTR=0 or -DCXXFEATURES_NULLPTR=nullptr.
> >> 
> >> See what Qt does for other similar API decisions on what should be
> >> defined to something (like nullptr, constexpr, final etc), and what
> >> should be a 'guard' define like above (eg lambdas, variadic templates
> >> etc).
> >> 
> >> Note also that by defining the CXXFEATURES_FINAL to something, you get to
> >> use the 'sealed' extension, which does the same thing, and works with
> >> VC2005. See qcompilerdetection.h.
> > 
> > The module returns just a list of CMake flags. How this is passed to the
> > user (header, defines, whatever) is currently something the user must
> > decide. I will not do anything fancy in the testcase for now.
> 
> Imagine I wanted to set Grantlee_FINAL to empty or final based on whether
> c++11 was active or not. How would I do that?
> 
> I might do this:
> 
>  # This seems like an API smell. With g++ I want to wrap the add_definitions
> # in a condition for enabling c++11 at all. Does this mean that c++11 #
> features are not made available when using MSVC?
>  if(CXX11_COMPILER_FLAGS)
>    # Can't use add_compile_options as CXX11_COMPILER_FLAGS is a string, not
>    # a list.
>    # This means that -std=c++11 is also passed when linking.
>    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX11_COMPILER_FLAGS}")
> 
>    if (CXXFeatures_class_override_final_FOUND)
>      add_definitions(-DGrantlee_FINAL=final)
>      set(_final_defined 1)
>    endif()
>  endif()
> 
>  if (NOT _final_defined)
>    add_definitions(-DGrantlee_FINAL=)
>  endif()
> 
> 
> Do you have any more-real-world examples of what code using your module
> would look like?
> 
> My c++ code would then look like:
> 
>  struct A Grantlee_FINAL
>  {
>    int data;
>  };
> 
> However, now downstreams need to define Grantlee_FINAL to something in order
> to compile. We can help of course by putting Grantlee_FINAL in the
> INTERFACE_COMPILE_DEFINITIONS of Grantlee.

That is a problem I did not try to solve now. The idea was to get your targets 
compiled. Even if your target is a library used by the outside and full of 
C++11 stuff you can keep the interface clean of C++11 specifics.

Having the interface in a full-featured C++11 way is step 2 or 3 on the way. I 
would like to solve the first step first.

Eike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://public.kitware.com/pipermail/cmake-developers/attachments/20130920/cabb5c6e/attachment.sig>


More information about the cmake-developers mailing list