小编Dan*_*rey的帖子

一个积极的lambda:'+ [] {}' - 这是什么巫术?

在Stack Overflow问题中,在C++ 11中不允许重新定义lambda,为什么?,给出了一个不编译的小程序:

int main() {
    auto test = []{};
    test = []{};
}
Run Code Online (Sandbox Code Playgroud)

问题得到了回答,一切似乎都很好.然后是Johannes Schaub并做了一个有趣的观察:

如果你+在第一个lambda之前放置一个,它会神奇地开始工作.

所以我很好奇:为什么以下工作呢?

int main() {
    auto test = +[]{}; // Note the unary operator + before the lambda
    test = []{};
}
Run Code Online (Sandbox Code Playgroud)

它与GCC 4.7+和Clang 3.2+都很好.代码标准是否符合要求?

c++ lambda operator-overloading language-lawyer c++11

255
推荐指数
1
解决办法
3万
查看次数

使用decltype获取表达式的类型,不使用const

考虑以下程序:

int main ()
{
    const int e = 10;

    for (decltype(e) i{0}; i < e; ++i) {
        // do something
    }
}
Run Code Online (Sandbox Code Playgroud)

这无法使用clang(以及gcc)进行编译:

decltype.cpp:5:35: error: read-only variable is not assignable
    for (decltype(e) i{0}; i < e; ++i) {
                                  ^ ~
Run Code Online (Sandbox Code Playgroud)

基本上,编译器假设i必须是const,因为e是.

有没有办法可以decltype用来获取类型e,但删除说明const符?

c++ decltype c++11

36
推荐指数
4
解决办法
8689
查看次数

为什么std :: array <T,0>不为空?

鉴于此std::array< T, 0 >,为什么它不是空的?我的意思是"空",如:

 std::is_empty< std::array< int, 0 > >::value
Run Code Online (Sandbox Code Playgroud)

回来false

 #include <iostream>
 #include <tuple>
 #include <array>

 struct Empty {};

 int main()
 {
     std::cout << sizeof(std::tuple<int>) << std::endl;
     std::cout << sizeof(std::tuple<int,Empty>) << std::endl;
     std::cout << sizeof(std::tuple<int,std::array<int,0>>) << std::endl;
 }
Run Code Online (Sandbox Code Playgroud)

产量

 4
 4
 8
Run Code Online (Sandbox Code Playgroud)

这意味着,std::array<int,0>没有应用空基础优化(EBO).

这对我来说似乎特别奇怪std::tuple<>(注意:没有模板参数)空的,即std::is_empty<std::tuple<>>::value产量true.

问题:为什么这样,因为这个尺寸0已经是一个特例了std::array?这是标准的故意还是疏忽?

c++ language-lawyer c++11

21
推荐指数
1
解决办法
1459
查看次数

是否可以创建模板别名?

请考虑以下代码:

template< template< typename ... > class ... Ts >
struct unite
{
    template< typename ... T >
    struct type
        : Ts< T ... > ...
    { };
};

// This does not work as ::type does not name a type, but a template:

// template< template< typename ... > class ... Ts >
// using unite_t = typename unite< Ts ... >::type;

template< typename > struct debug_none {};
template< typename > struct debug_cout {};

template< typename ... > struct …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 type-alias c++14

13
推荐指数
1
解决办法
385
查看次数

如何移动initializer_list的元素?

假设您有一个类型变量,std::vector<std::string>并使用初始化列表初始化它:

using V = std::vector<std::string>;
V v = { "Hello", "little", "world", "of", "move", "semantics" };
Run Code Online (Sandbox Code Playgroud)

编译器将为std::string每个字符串文字创建一个临时文件,在这些文件上创建一个初始化列表,然后调用ctor V并创建向量.ctor不知道所有这些字符串都是临时字符串,所以它正在复制每个字符串.

我没有在标准中找到任何允许矢量ctor在临时时移动元素的东西.

我错过了什么或使用初始化程序列表导致不必要的副本?我正在编写类,这个问题可能会导致代码效率低下.任何避免不必要的副本的技术将不胜感激.

c++ initializer-list move-semantics c++11

13
推荐指数
1
解决办法
919
查看次数

有没有像std :: size()这样的函数?

任意给定类型T的内置阵列X,也有功能std::begin()std::end()我可以打电话,但为什么是不是有std::size()?似乎奇怪没有那个.

我可以使用std::end(x)-std::begin(x),但仍然std::size(x)会更好.

是的,我知道这些std::vectorstd::array课程.这只是一个问题,为什么STL中还没有这样简单的东西.

c++ c++11

12
推荐指数
2
解决办法
4154
查看次数

正确签名/检测Container :: reserve()的存在

给定一个C符合STL的容器类型,如何正确检测是否C包含成员函数reserve?我尝试了以下方法(使用GCC 4.6.3):

template< typename C, typename = void >
struct has_reserve
  : std::false_type
{};

template< typename C >
struct has_reserve< C, typename std::enable_if<
                         std::is_same<
                           decltype( &C::reserve ),
                           void (C::*)( typename C::size_type )
                         >::value
                       >::type >
  : std::true_type
{};
Run Code Online (Sandbox Code Playgroud)

这适用于C存在std::vector,但不适用于无序容器,例如std::unordered_set.其原因是,这reserve是一个的(直接)成员函数std::vector,但是对于无序的容器它是从基类继承的,即,它的签名是不void (C::*)( typename C::size_type )void (B::*)( typename C::size_type )对于一些未指定的基类BC.

我知道如何解决它并检测reserve即使是遗传,但它看起来很笨拙,我想知道标准允许什么.所以...

我的问题是:标准是允许reserve从未指定的基类继承还是概要绑定并需要直接成员函数?

c++ std enable-if c++11

11
推荐指数
1
解决办法
795
查看次数

C++中的const-correctness语义

为了fun and profit™,我正在用C++编写一个trie类(使用C++ 11标准.)

trie<T>有一个迭代器,trie<T>::iterator.(它们实际上都是函数 const_iterator s,因为你无法修改trie value_type.)迭代器的类声明看起来像这样:

template<typename T>
class trie<T>::iterator : public std::iterator<std::bidirectional_iterator_tag, T> {
    friend class trie<T>;
    struct state {
        state(const trie<T>* const node, const typename std::vector<std::pair<typename T::value_type, std::unique_ptr<trie<T>>>>::const_iterator& node_map_it ) :
            node{node}, node_map_it{node_map_it} {}
// This pointer is to const data:
        const trie<T>* node;
        typename std::vector<std::pair<typename T::value_type, std::unique_ptr<trie<T>>>>::const_iterator node_map_it;
    };
public:
    typedef const T value_type;
    iterator() =default;
    iterator(const trie<T>* node) {
        parents.emplace(node, node->children.cbegin());
            // ...
    }
    // ...
private:
    std::stack<state> …
Run Code Online (Sandbox Code Playgroud)

c++ iterator c++11

11
推荐指数
1
解决办法
576
查看次数

验证GCC中的错误

我想验证以下是GCC中的错误,而不是我对C++的理解.请考虑以下代码:

struct A
{
    struct B
    {
        template< typename U > U as() const { return U(); }
    };

    B operator[]( int ) const { return B(); }
};

template< typename T >
struct as
{
    template< typename U >
    static T call( const U& u )
    {
        return u[ 0 ].as< T >(); // accepted by Clang 3.2, rejected by GCC 4.7
        // return u[ 0 ].template as< T >(); // does not help and is IMHO not needed …
Run Code Online (Sandbox Code Playgroud)

c++ gcc templates c++11

10
推荐指数
1
解决办法
434
查看次数

在VS 2013中使用初始化列表初始化地图的地图

我正在尝试使用C++ 11初始化地图地图.我的编译器是VS 2013 Express.

unordered_map<EnumType, unordered_map<string, string>> substitutions = {
    {
        Record::BasementType,
        {
            { "0", "" },
            { "1", "Slab or pier" },
            { "2", "Crawl" }
        }
    },
    {
        Record::BuildingStyle,
        {
            { "0", "" },
            { "1", "Ranch" },
            { "2", "Raised ranch" }
        }
    },
    // ... and so on
};
Run Code Online (Sandbox Code Playgroud)

它是编译但我在ntdll.dll中获得断点.但是这段代码的简化版本:

unordered_map<EnumType, unordered_map<string, string>> substitutions = {
    {
        Record::BasementType,
        {
            { "0", "" },
            { "1", "Slab or pier" },
            { "2", "Crawl" }
        }
    },
    // …
Run Code Online (Sandbox Code Playgroud)

c++ stl c++11 visual-studio-2013

10
推荐指数
1
解决办法
2458
查看次数