我在Wandbox中尝试了以下代码:
#include <array>
#include <iostream>
#include <tuple>
#include <typeinfo>
#include <functional>
#include <utility>
int main()
{
constexpr std::array<const char, 10> str{"123456789"};
constexpr auto foo = std::apply([](auto... args) constexpr { std::integer_sequence<char, args...>{}; } , str);
std::cout << typeid(foo).name();
}
Run Code Online (Sandbox Code Playgroud)
并且编译器告诉我那args...不是常量表达式.怎么了?
我在http://en.cppreference.com/w/cpp/language/value_initialization中读到了关于值初始化的内容,但我对以下示例代码感到困惑:
struct A {
int i;
A() {} // user-provided ctor, does not initialize i
};
struct B { A a; }; // no user-provided ctor
std::cout << B().a.i << '\n'; // value-initializes a B temporary
// leaves b.a.i uninitialized in C++03
// sets b.a.i to zero in C++11
// (note that B{}.a.i leaves b.a.i uninitialized in C++14, but for
// a different reason: C++14's B{} is aggregate-initialization)
Run Code Online (Sandbox Code Playgroud)
cppreference说:
在所有情况下,如果使用空的大括号{}并且T是聚合类型
that is not a class type with a …
我发现GCC 7已经实现了保证副本省略,我在wandbox中尝试了下面的代码:
#include <iostream>
struct NonMovable
{
NonMovable() noexcept = default;
NonMovable(NonMovable&&) noexcept = delete;
NonMovable& operator=(NonMovable&&) noexcept = delete;
};
NonMovable Make()
{
return {};
}
int main()
{
//[[maybe_unused]] const auto x = Make();
//const auto z = NonMovable{};
[[maybe_unused]] const auto y = NonMovable{NonMovable{}};
}
Run Code Online (Sandbox Code Playgroud)
我得到编译错误:
prog.cc: In function 'int main()':
prog.cc:20:60: error: use of deleted function 'NonMovable::NonMovable(NonMovable&&)'
[[maybe_unused]] const auto y = NonMovable{NonMovable{}};
^
prog.cc:6:5: note: declared here
NonMovable(NonMovable&&) noexcept = delete;
^~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
根据 …
我想得到名字以'T'开头的人数:
#include <iostream>
#include <string>
#include <range\v3\all.hpp>
using namespace ranges;
int main()
{
const auto names = std::vector<std::string> {"Tony", "Peter"};
std::cout << size(names | view::filter([](const auto& s) {return s[0] == 'T';}));
}
Run Code Online (Sandbox Code Playgroud)
但我得到了巨大的编译错误:
? clang -std=c++14 test.cpp
test.cpp:11:18: error: no matching function for call to object of type 'const ranges::v3::adl_size_detail::size_fn'
std::cout << size(names | view::filter([](const auto& s) {return s[0] == 'T';}));
^~~~
K:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\range/v3/size.hpp:90:32: note: candidate template
ignored: substitution failure [with Rng =
ranges::v3::remove_if_view<ranges::v3::iterator_range<std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<std::basic_string<char,
std::char_traits<char>, std::allocator<char> …Run Code Online (Sandbox Code Playgroud) 我想实现unordered_map类似于std的.所以,我期待在源代码中<unordered_map>,并<xhash>在Visual C++ 2013年我找到了实现调用_Init该函数unordered_map的构造.我发现函数的定义如下:
void _Init(size_type _Buckets = _Min_buckets)
{ // initialize hash table with _Buckets buckets, leave list alone
_Vec.assign(2 * _Buckets, _Unchecked_end());
_Mask = _Buckets - 1;
_Maxidx = _Buckets;
}
Run Code Online (Sandbox Code Playgroud)
该函数_Unchecked_end()只返回_List.Unchecked_end():
_Unchecked_iterator _Unchecked_end()
{ // return iterator for end of mutable sequence
return (_List._Unchecked_end());
}
Run Code Online (Sandbox Code Playgroud)
和begin()的std::unordered_map只是返回_List.begin()...
我认为只有一个列表的find()功能unordered_map在一般情况下不能满足常数的复杂性.
那么...... VC++如何实现std::unordered_map呢?
对不起,我没有说清楚.在我看来,实现unordered_map应该是一个带有 …