1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
10
11// <fstream>
12
13// class basic_filebuf;
14
15// native_handle_type native_handle() const noexcept;
16
17#include <cassert>
18#include <fstream>
19#include <filesystem>
20#include <utility>
21
22#include "platform_support.h"
23#include "test_macros.h"
24#include "../native_handle_test_helpers.h"
25
26template <typename CharT>
27void test() {
28 std::basic_filebuf<CharT> f;
29 std::filesystem::path p = get_temp_file_name();
30
31 // non-const
32 {
33 assert(f.open(p, std::ios_base::in) != nullptr);
34 std::same_as<NativeHandleT> decltype(auto) handle = f.native_handle();
35 assert(is_handle_valid(handle));
36 f.close();
37 assert(!is_handle_valid(handle));
38 static_assert(noexcept(f.native_handle()));
39 }
40 // const
41 {
42 assert(f.open(p, std::ios_base::in) != nullptr);
43 std::same_as<NativeHandleT> decltype(auto) const_handle = std::as_const(f).native_handle();
44 assert(is_handle_valid(const_handle));
45 f.close();
46 assert(!is_handle_valid(const_handle));
47 static_assert(noexcept(std::as_const(f).native_handle()));
48 }
49}
50
51int main(int, char**) {
52 test<char>();
53#ifndef TEST_HAS_NO_WIDE_CHARACTERS
54 test<wchar_t>();
55#endif
56
57 return 0;
58}
59

source code of libcxx/test/std/input.output/file.streams/fstreams/filebuf.members/native_handle.pass.cpp