Files
libcxx/test/std/diagnostics/syserr/syserr.hash/error_code.pass.cpp
Stephan T. Lavavej 4d6f42350e [libcxx] [test] Fix size_t-to-int truncation warnings in syserr.hash.
After r289363, these tests were triggering MSVC x64 warning C4267
"conversion from 'size_t' to 'int', possible loss of data" by taking 0, 2, and 10
as std::size_t, then constructing error_code(int, const error_category&) or
error_condition(int, const error_category&) from that (N4618 19.5.3.2
[syserr.errcode.constructors]/3, 19.5.4.2 [syserr.errcondition.constructors]/3).

The fix is simple: take these ints as int, pass them to the int-taking
constructor, and perform a value-preserving static_cast<std::size_t>
when comparing them to `std::size_t result`.

Fixes D27691.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@289512 91177308-0d34-0410-b5e6-96231b3b80d8
2016-12-13 01:54:58 +00:00

45 lines
1.0 KiB
C++

//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <functional>
// template <class T>
// struct hash
// : public unary_function<T, size_t>
// {
// size_t operator()(T val) const;
// };
#include <system_error>
#include <cassert>
#include <type_traits>
#include "test_macros.h"
void
test(int i)
{
typedef std::error_code T;
typedef std::hash<T> H;
static_assert((std::is_same<H::argument_type, T>::value), "" );
static_assert((std::is_same<H::result_type, std::size_t>::value), "" );
H h;
T ec(i, std::system_category());
const std::size_t result = h(ec);
LIBCPP_ASSERT(result == static_cast<std::size_t>(i));
((void)result); // Prevent unused warning
}
int main()
{
test(0);
test(2);
test(10);
}