Your Search Results

    Using C++ in Mozilla code

    This page is a draft for expository and exploratory purposes. Do not trust the information listed here.

    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. 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 4.8.1 2.9 No
    member initializers 2013 4.7 3.0 No
    variadic templates 2013 4.3 2.9 No
    Initializer lists 2013 4.4 3.1 No
    static_assert 2010 4.3 2.9 Yes
    auto 2010 4.4 2.9 Yes
    lambdas 2010 4.5 3.1 No
    decltype 2010 4.3 2.9 Include mozilla/Types.h
    Foo<Bar>> 2010 4.3 2.9 Yes
    auto func() -> int 2010 4.4 3.1 Only when needed to avoid global-scope typedefs
    Templated aliasing 2013 4.7 3.0 No
    nullptr 2010 4.6 3.0 Include mozilla/NullPtr.h
    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 Use macros from mozilla/TypedEnum.h
    enum foo; 2012 4.6 3.1 No
    [[attributes]] 4.8 3.3 No (see notes)
    constexpr 4.6 3.1 Use MOZ_CONSTEXPR from mozilla/Attributes.h
    alignof/alignas 4.8 3.3 No (see notes)
    Delegated constructors 2013 4.7 3.0 No
    Inherited constructors 4.8 3.3 No
    explicit operator bool() 2013 4.5 3.0 No
    char16_t/u"string" 4.4 3.0 Use MOZ_UTF16("string") instead of u"string".
    r"string" 2013 4.5 3.0 No
    operator""() 4.7 3.1 No
    =delete 2013 4.4 2.9 Use MOZ_DELETE from mozilla/Attributes.h
    =default 2013 4.4 3.0 No
    unrestricted unions 4.6 3.1 No
    for (auto x : vec) 2012 4.6 3.0 No
    override/final 2012 4.7 3.0 Use MOZ_OVERRIDE and MOZ_FINAL from mozilla/Attributes.h
    thread_local 4.8 3.3 No (see notes)
    function template default arguments 2013 4.3 2.9 No
    local structs as template parameters 2010 4.5 2.9  
    0b100 (C++1y) 4.9 2.9 No
    Generic lambdas (C++1y) No
    Variable templates (C++1y) No


    rvalue references: Implicit move method generation cannot be used. Temporaries may bind to lvalues in some cases on MSVC.

    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.

    As a general rule of thumb, prefer the use of MFBT or XPCOM APIs to standard C++ APIs. Some of our APIs include extra methods not found in the standard API (such as those reporting the size of data structures). A complete listing of MFBT APIs can be found in mfbt/; the XPCOM APIs can be found in xpcom/base, xpcom/ds, and xpcom/glue. What follows is a list of permissible standard C++ and Mozilla library calls for various tasks, broken down by function.

    Data structures and algorithms

    Name Header MSVC libstdc++ libc++ STLport Purpose Notes
    std::array <array>         Compile-time sized sequence  
    std::forward_list <forward_list>         Singly-linked list  
    std::unordered_map <unordered_map>         Map backed by a hashtable  
    std::unordered_multimap <unordered_map>         Multimap backed by a hashtable  
    std::unordered_set <unordered_set>         Set backed by a hashtable  
    std::unordered_multiset <unordered_set>         Multiset backed by a hashtable  
    std::next/std::prev <iterator>         Increment iterators a set distance  
    std::begin/std::end <iterator>         Ranged iterator  
    std::cbegin/std::cend (C++14) <iterator>         Const ranged iterator  
    emplace* methods various            
    std::all_of/std::any_of/std::none_of <algorithm>            
    std::find_if_not <algorithm>            
    std::is_* methods <algorithm>            
    std::shuffle <algorithm>            
    std::iota <algorithm>            
    std::minmax <algorithm>            


    Name Header MSVC libstdc++ libc++ STLport Purpose Notes
    std::u16string, std::u32string <string>            
    std::codecvt_utf8 <codecvt>            


    Don't use C++'s I/O functionality, particularly std::cin and std::cout. The interface is necessarily synchronous, and it causes static constructors to be added. Instead, use the stream APIs developed in XPCOM for input and output.

    Platform support (POSIX/WinAPI)

    Name Header MSVC libstdc++ libc++ STLport Purpose Notes
    std::chrono::duration <chrono>            
    std::chrono::*_clock <chrono>            
    std::chrono::time_point <chrono>            
    std::atomic <atomic> 2012 4.4 3.0     Use mozilla::Atomic from mozilla/Atomics.h instead.
    std::thread <thread>            
    std::mutex <mutex>            
    std::shared_mutex (C++14) <mutex>            
    std::lock_guard <mutex>            
    std::shared_lock (C++14) <mutex>            
    std::call_once <mutex>            
    std::condition_variable <condition_variable>            
    std::promise <future>            
    std::future <future>            
    std::async <future>            


    sec 18: <limits> <new> <typeinfo> <exception> <initializer_list>

    sec 19: <stdexcept> <system_error>

    sec 20: <utility> <tuple> <memory> <functional> <type_traits> <ratio> <scoped_allocator> <type_index>

    sec 26: <complex> <random> <numeric>

    sec 28: <regex>

    Document Tags and Contributors

    Contributors to this page: Ehsan, froydnj, botond, Jcranmer
    Last updated by: Ehsan,