Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

C++ language features

Mozilla code only uses a subset of C++. Runtime type information (RTTI) is disabled, as it tends to cause a very large increase in codesize. This means that dynamic_cast, typeid() and <typeinfo> cannot be used in Mozilla code. Also disabled are exceptions; do not use try/catch or throw any exceptions. Libraries that throw exceptions may be used if you are willing to have the throw instead be treated as an abort.

As of Mozilla 25, C++11 mode is required to build Mozilla. As of Mozilla 48, gcc 4.8 is required on all platforms. As of Mozilla 50, MSVC 2015 Update 2 is required on Windows. This means that C++11 can be used where supported on all platforms. The list of acceptable features is given below:

  MSVC GCC Clang  
Current minimal requirement 2015 4.8 3.6  
Feature MSVC GCC Clang Can be used in code
type_t && 2010 4.3 2.9 Yes (see notes)
ref qualifiers on methods 2015 4.8.1 2.9 Yes
member initializers 2013 4.7 3.0 Yes
variadic templates 2013 4.3 2.9 Yes
Initializer lists 2013 4.4 3.1 Yes
static_assert 2010 4.3 2.9 Yes
auto 2010 4.4 2.9 Yes
lambdas 2010 4.5 3.1 Yes
decltype 2010 4.3 2.9 Yes
Foo<Bar<T>> 2010 4.3 2.9 Yes
auto func() -> int 2010 4.4 3.1 Yes
Templated aliasing 2013 4.7 3.0 Yes
nullptr 2010 4.6 3.0 Yes, but use decltype(nullptr) instead of std::nullptr_t.
enum foo : int16_t {}; 2008 4.4 2.9 Yes
enum class foo {}; 2012 4.4 2.9 Yes
enum foo; 2012 4.6 3.1 Yes
[[attributes]] 2015 4.8 3.3 No (see notes)
constexpr 2015 4.6 3.1 Yes
alignas 2015 4.8 3.3 Yes
alignof 2015 4.8 3.3 Yes, but see notes ; only clang 3.6 claims __has_feature(cxx_alignof)
Delegated constructors 2013 4.7 3.0 Yes
Inherited constructors 2015 4.8 3.3 Yes
explicit operator bool() 2013 4.5 3.0 Yes
char16_t/u"string" 2015 4.4 3.0 Yes
R"(string)" 2013 4.5 3.0 Yes
operator""() 2015 4.7 3.1 Yes
=delete 2013 4.4 2.9 Yes
=default 2013 4.4 3.0 Yes
unrestricted unions 2015 4.6 3.1 Yes
for (auto x : vec) 2012 4.6 3.0 Yes (be careful about the type of the iterator)
override/final 2012 4.7 3.0 Yes
thread_local 2015 4.8 3.3 No (see notes)
function template default arguments 2013 4.3 2.9 Yes
local structs as template parameters 2010 4.5 2.9 Yes
extended friend declarations 2010 4.7 2.9 Yes
0b100 (C++14) 2015 4.9 2.9 No
Return type deduction (C++14) 2015 4.9 3.4 No
Generic lambdas (C++14) 2015 4.9 3.4 No
Initialized lambda captures (C++14) 2015 4.9 3.4 No
Digit separator (C++14) 2015 4.9 3.4 No
Variable templates (C++14) 5.0 3.4 No
Relaxed constexpr (C++14) 5.0 3.4 No
Concepts (Concepts TS) 6.0 No
Inline variables (C++17) No
constexpr_if (C++17) No
constexpr lambdas (C++17) No

Notes

rvalue references: Implicit move method generation cannot be used.

Attributes: Several common attributes are defined in mozilla/Attributes.h or nscore.h.

Alignment: Some alignment utilities are defined in mozilla/Alignment.h. /!\ MOZ_ALIGNOF and alignof don't have the same semantics. Be careful of what you expect from them.

Thread locals: There is a thread-local variable helper in mozilla/ThreadLocal.h. In addition, the native thread_local of MSVC2015 may break Windows XP support. See bug 1204752. The native thread_local of clang may break OSX 10.6, too, but support for OSX 10.6 has been retired.

C++14: C++14 is not enabled on compilers that support it.

C++ and Mozilla standard libraries

The Mozilla codebase contains within it several subprojects which follow different rules for which libraries can and can't be used it. The rules listed here apply to normal platform code, and assume unrestricted usability of MFBT or XPCOM APIs.

The rest of this section is a draft for expository and exploratory purposes. Do not trust the information listed here.

What follows is a list of standard library components provided by Mozilla or the C++ standard. If an API is not listed here, then it is not permissible to use it in Mozilla code. Deprecated APIs are not listed here. In general, prefer Mozilla variants of data structures to standard C++ ones, even when permitted to use the latter, since Mozilla variants tend to have features not found in the standard library (e.g., memory size tracking) or have more controllable performance characteristics.

Data structures

Name Header STL equivalent Notes
nsAutoTArray nsTArray.h   Like nsTArray, but will store a small amount as stack storage
nsAutoTObserverArray nsTObserverArray.h   Like nsTObserverArray, but will store a small amount as stack storage
mozilla::BloomFilter mozilla/BloomFilter.h   Probabilistic set membership (see Wikipedia)
nsClassHashtable nsClassHashtable.h   Adaptation of nsTHashtable, see XPCOM hashtable guide
nsCOMArray nsCOMArray.h   Like nsTArray<nsCOMPtr<T>>
nsDataHashtable nsClassHashtable.h std::unordered_map Adaptation of nsTHashtable, see XPCOM hashtable guide
nsDeque nsDeque.h std::deque  
mozilla::EnumSet mozilla/EnumSet.h   Like std::set, but for enum classes.
nsInterfaceHashtable nsInterfaceHashtable.h std::unordered_map Adaptation of nsTHashtable, see XPCOM hashtable guide
nsJSThingHashtable nsJSThingHashtable.h   Adaptation of nsTHashtable, see XPCOM hashtable guide
mozilla::LinkedList mozilla/LinkedList.h std::list Doubly-linked list
nsRefPtrHashtable nsRefPtrHashtable.h std::unordered_map Adaptation of nsTHashtable, see XPCOM hashtable guide
mozilla::SegmentedVector mozilla/SegmentedVector.h std::deque Doubly-linked list of vector elements
mozilla::SplayTree mozilla/SplayTree.h   Quick access to recently-accessed elements (see Wikipedia)
nsTArray nsTArray.h std::vector  
nsTHashtable nsTHashtable.h std::unordered_map See XPCOM hashtable guide, you probably want a subclass
nsTObserverArray nsTObserverArray.h   Like nsTArray, but iteration is stable even through mutation
nsTPriorityQueue nsTPriorityQueue.h std::priority_queue Unlike the STL class, not a container adapter
mozilla::Vector mozilla/Vector.h std::vector  

Untranslated notes:

ns*String - too complicated to explain

Safety utilities:
mozilla::Array mfbt/Array.h (safe array index)
mozilla::AssertedCast mfbt/Casting.h (casts)
mozilla::CheckedInt mfbt/CheckedInt.h (avoids overflow)
nsCOMPtr xpcom/glue/nsCOMPtr.h (~= std::shared_ptr)
mozilla::EnumeratedArray mfbt/EnumeratedArray.h (mozilla::Array, for enum types)
mozilla::RangedPtr mfbt/RangedPtr.h (prevents out-of-bounds)
mozilla::RefPtr mfbt/RefPtr.h (~= std::shared_ptr)
nsRefPtr xpcom/base/nsRefPtr.h (~= std::shared_ptr)
StaticRefPtr xpcom/base/StaticPtr.h (nsRefPtr w/o static constructor)
mozilla::UniquePtr mfbt/UniquePtr.h (~= std::unique_ptr)
mozilla::WeakPtr mfbt/WeakPtr.h (~= std::weak_ptr)
nsWeakPtr xpcom/base/nsWeakPtr.h (~= std::weak_ptr)

Algos:
mozilla::BinarySearch mfbt/BinarySearch.h
mozilla::BitwiseCast mfbt/Casting.h (strict aliasing-safe cast)
mozilla/MathAlgorithms.h (rotate, ctlz, popcount, gcd, abs, lcm)
mozilla::RollingMean mfbt/RollingMean.h ()

Misc:
mozilla::AlignedStorage mfbt/Alignment.h (replacement for std::aligned_storage)
mozilla::Atomic mfbt/Atomic.h (replacement for std::atomic)
mozilla::CondVar xpcom/glue/CondVar.h
mozilla::Maybe mfbt/Maybe.h (std::optional)
mozilla::MaybeOneOf mfbt/MaybeOneOf.h (~mozilla::Maybe<union {T1, T2}>)
mozilla::Monitor xpcom/glue/Monitor.h
mozilla::Mutex xpcom/glue/Mutex.h
mozilla::Pair mfbt/Pair.h (~= std::tuple<T1, T2> -- minimal space!)
mozilla::ReentrantMonitor xpcom/glue/ReentrantMonitor.h
mozilla::StaticMutex xpcom/base/StaticMutex.h
mozilla::TimeStamp xpcom/ds/TimeStamp.h (~= std::chrono::time_point)

mozilla/ArrayUtils.h
mozilla/Compression.h
mozilla/Endian.h
mozilla/FloatingPoint.h
mozilla/HashFunctions.h (~= std::hash)
mozilla/Move.h (std::move, std::swp, std::Forward)
mozilla/PodOperations.h (C++ versions of memset, memcpy, etc.)
mozilla/TypeTraits.h (replacement for <type_traits>)

(TODO: make list of STL usability here)

Document Tags and Contributors

 Last updated by: glandium,