Using C++ in Mozilla code

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 37, MSVC 2013 Update 3 is required on Windows; as of Mozilla 38, gcc 4.7 is required on all platforms. This means that C++11 can be used where supported on all platforms. The list of acceptable features is given below:

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 No
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>> 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]] 4.8 3.3 No (see notes)
constexpr 2015 4.6 3.1 Use MOZ_CONSTEXPR from mozilla/Attributes.h
alignof/alignas 2015 4.8 3.3 No (see notes)
Delegated constructors 2013 4.7 3.0 Yes
Inherited constructors 2015 4.8 3.3 No
explicit operator bool() 2013 4.5 3.0 Yes
char16_t/u"string" 2015 4.4 3.0 Use MOZ_UTF16("string") instead of u"string".
r"string" 2013 4.5 3.0 Yes
operator""() 2015 4.7 3.1 No
=delete 2013 4.4 2.9 Yes
=default 2013 4.4 3.0 Yes
unrestricted unions 2015 4.6 3.1 No
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
0b100 (C++14) 2015 4.9 2.9 No
Generic lambdas (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
Terse range-based for loops (C++1z) 2015 3.5 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.

Thread locals: There is a thread-local variable helper in mozilla/ThreadLocal.h.

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::MaybeOnOf 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

Contributors to this page: jrmuizel, bobowen, Waldo, Jcranmer, Ehsan, Steffen, froydnj, botond
Last updated by: jrmuizel,