小编Cu2*_*u2S的帖子

std :: apply和constant表达式?

我在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...不是常量表达式.怎么了?

c++ c++17 stdapply

9
推荐指数
3
解决办法
592
查看次数

对C++ 11和14之间的价值初始化的区别感到困惑

我在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 …

c++ c++11 c++14

8
推荐指数
0
解决办法
120
查看次数

保证副本省略和不可移动{不可移动{}}

我发现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)

根据 …

c++ gcc copy-elision c++17

6
推荐指数
1
解决办法
423
查看次数

为什么我不能在范围-v3中获得范围的大小?

我想得到名字以'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)

c++ clang c++11 c++14 range-v3

5
推荐指数
2
解决办法
1024
查看次数

Visual C++只用一个std :: list来实现std :: unordered_map?

我想实现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应该是一个带有 …

c++ c++11

0
推荐指数
1
解决办法
875
查看次数

标签 统计

c++ ×5

c++11 ×3

c++14 ×2

c++17 ×2

clang ×1

copy-elision ×1

gcc ×1

range-v3 ×1

stdapply ×1