Utilização de C++ no código da Mozilla

Esta tradução não está completa. Por favor ajude a traduzir este artigo a partir do Inglês.

Caraterísticas da linguagem C++

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. As of Mozilla 48, gcc 4.8 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:

  MSVC GCC Clang  
Requisito mínimo atual 2013 4.8 3.4  
Funcionalidade MSVC GCC Clang Pode ser utilizada no código
type_t && 2010 4.3 2.9 Sim (consultar notas)
ref qualifiers on methods 2015 4.8.1 2.9 Não
member initializers 2013 4.7 3.0 Sim
variadic templates 2013 4.3 2.9 Sim
Initializer lists 2013 4.4 3.1 Sim
static_assert 2010 4.3 2.9 Sim
auto 2010 4.4 2.9 Sim
lambdas 2010 4.5 3.1 Sim
decltype 2010 4.3 2.9 Sim
Foo<Bar<T>> 2010 4.3 2.9 Sim
auto func() -> int 2010 4.4 3.1 Sim
Templated aliasing 2013 4.7 3.0 Sim
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 Sim
enum class foo {}; 2012 4.4 2.9 Sim
enum foo; 2012 4.6 3.1 Sim
[[attributes]] 2015 4.8 3.3 Não (consultar notas)
constexpr 2015 4.6 3.1 Use MOZ_CONSTEXPR from mozilla/Attributes.h
alignof/alignas 2015 4.8 3.3 No (see notes) ; only clang 3.6 claims __has_feature(cxx_alignof)
Delegated constructors 2013 4.7 3.0 Sim
Inherited constructors 2015 4.8 3.3 Não
explicit operator bool() 2013 4.5 3.0 Sim
char16_t/u"string" 2015 4.4 3.0 Use MOZ_UTF16("string") instead of u"string".
R"(string)" 2013 4.5 3.0 Sim
operator""() 2015 4.7 3.1 Não
=delete 2013 4.4 2.9 Sim
=default 2013 4.4 3.0 Sim
unrestricted unions 2015 4.6 3.1 Não
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 Sim
thread_local 2015 4.8 3.3 Não (consultar notas)
function template default arguments 2013 4.3 2.9 Sim
local structs as template parameters 2010 4.5 2.9 Sim
extended friend declarations 2010 4.7 2.9 Sim
0b100 (C++14) 2015 4.9 2.9 Não
Return type deduction (C++14) 2015 4.9 3.4 Não
Generic lambdas (C++14) 2015 4.9 3.4 Não
Initialized lambda captures (C++14) 2015 4.9 3.4 Não
Digit separator (C++14) 2015 4.9 3.4 Não
Variable templates (C++14) 5.0 3.4 Não
Relaxed constexpr (C++14) 5.0 3.4 Não
Concepts (Concepts TS) 6.0 Não
Inline variables (C++17) Não
constexpr_if (C++17) Não
constexpr lambdas (C++17) Não


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. 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.

Blibliotecas padrão de C++ e Mozilla

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.

Estruturas de dados

Nome Cabeçalho STL equivalente 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)

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 ()

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/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)

Etiquetas do documento e contribuidores

 Última atualização por: mansil,