CXX11Workarounds.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2013 Christian Seiler <christian@iwakd.de>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_CXX11WORKAROUNDS_H
11 #define EIGEN_CXX11WORKAROUNDS_H
12 
13 /* COMPATIBILITY CHECKS
14  * (so users of compilers that are too old get some realistic error messages)
15  */
16 #if defined(__INTEL_COMPILER) && (__INTEL_COMPILER < 1310)
17 #error Intel Compiler only supports required C++ features since version 13.1.
18 // note that most stuff in principle works with 13.0 but when combining
19 // some features, at some point 13.0 will just fail with an internal assertion
20 #elif defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6))
21 // G++ < 4.6 by default will continue processing the source files - even if we use #error to make
22 // it error out. For this reason, we use the pragma to make sure G++ aborts at the first error
23 // it sees. Unfortunately, that is still not our #error directive, but at least the output is
24 // short enough the user has a chance to see that the compiler version is not sufficient for
25 // the funky template mojo we use.
26 #pragma GCC diagnostic error "-Wfatal-errors"
27 #error GNU C++ Compiler (g++) only supports required C++ features since version 4.6.
28 #endif
29 
30 /* Check that the compiler at least claims to support C++11. It might not be sufficient
31  * because the compiler may not implement it correctly, but at least we'll know.
32  */
33 #if __cplusplus <= 199711L
34 #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
35 #pragma GCC diagnostic error "-Wfatal-errors"
36 #endif
37 #error This library needs at least a C++11 compliant compiler. If you use g++/clang, please enable the -std=c++11 compiler flag. (-std=c++0x on older versions.)
38 #endif
39 
40 namespace Eigen {
41 
42 // Use std::array as Eigen array
43 template <typename T, std::size_t N> using array = std::array<T, N>;
44 
45 namespace internal {
46 
47 /* std::get is only constexpr in C++14, not yet in C++11
48  * - libstdc++ from version 4.7 onwards has it nevertheless,
49  * so use that
50  * - libstdc++ older versions: use _M_instance directly
51  * - libc++ all versions so far: use __elems_ directly
52  * - all other libs: use std::get to be portable, but
53  * this may not be constexpr
54  */
55 #if defined(__GLIBCXX__) && __GLIBCXX__ < 20120322
56 #define STD_GET_ARR_HACK a._M_instance[I]
57 #elif defined(_LIBCPP_VERSION)
58 #define STD_GET_ARR_HACK a.__elems_[I]
59 #else
60 #define STD_GET_ARR_HACK std::template get<I, T, N>(a)
61 #endif
62 
63 template<std::size_t I, class T, std::size_t N> constexpr inline T& array_get(std::array<T,N>& a) { return (T&) STD_GET_ARR_HACK; }
64 template<std::size_t I, class T, std::size_t N> constexpr inline T&& array_get(std::array<T,N>&& a) { return (T&&) STD_GET_ARR_HACK; }
65 template<std::size_t I, class T, std::size_t N> constexpr inline T const& array_get(std::array<T,N> const& a) { return (T const&) STD_GET_ARR_HACK; }
66 
67 template<std::size_t I, class T> constexpr inline T& array_get(std::vector<T>& a) { return a[I]; }
68 template<std::size_t I, class T> constexpr inline T&& array_get(std::vector<T>&& a) { return a[I]; }
69 template<std::size_t I, class T> constexpr inline T const& array_get(std::vector<T> const& a) { return a[I]; }
70 
71 #undef STD_GET_ARR_HACK
72 
73 template <typename T> struct array_size;
74 template<class T, std::size_t N> struct array_size<const std::array<T,N> > {
75  static const size_t value = N;
76 };
77 template <typename T> struct array_size;
78 template<class T, std::size_t N> struct array_size<std::array<T,N> > {
79  static const size_t value = N;
80 };
81 
82 /* Suppose you have a template of the form
83  * template<typename T> struct X;
84  * And you want to specialize it in such a way:
85  * template<typename S1, typename... SN> struct X<Foo<S1, SN...>> { ::: };
86  * template<> struct X<Foo<>> { ::: };
87  * This will work in Intel's compiler 13.0, but only to some extent in g++ 4.6, since
88  * g++ can only match templates called with parameter packs if the number of template
89  * arguments is not a fixed size (so inside the first specialization, referencing
90  * X<Foo<Sn...>> will fail in g++). On the other hand, g++ will accept the following:
91  * template<typename S...> struct X<Foo<S...>> { ::: }:
92  * as an additional (!) specialization, which will then only match the empty case.
93  * But Intel's compiler 13.0 won't accept that, it will only accept the empty syntax,
94  * so we have to create a workaround for this.
95  */
96 #if defined(__GNUC__) && !defined(__INTEL_COMPILER)
97 #define EIGEN_TPL_PP_SPEC_HACK_DEF(mt, n) mt... n
98 #define EIGEN_TPL_PP_SPEC_HACK_DEFC(mt, n) , EIGEN_TPL_PP_SPEC_HACK_DEF(mt, n)
99 #define EIGEN_TPL_PP_SPEC_HACK_USE(n) n...
100 #define EIGEN_TPL_PP_SPEC_HACK_USEC(n) , n...
101 #else
102 #define EIGEN_TPL_PP_SPEC_HACK_DEF(mt, n)
103 #define EIGEN_TPL_PP_SPEC_HACK_DEFC(mt, n)
104 #define EIGEN_TPL_PP_SPEC_HACK_USE(n)
105 #define EIGEN_TPL_PP_SPEC_HACK_USEC(n)
106 #endif
107 
108 } // end namespace internal
109 
110 } // end namespace Eigen
111 
112 #endif // EIGEN_CXX11WORKAROUNDS_H
113 
114 /*
115  * kate: space-indent on; indent-width 2; mixedindent off; indent-mode cstyle;
116  */
Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13