小编Ric*_*ges的帖子

std :: move of string literal - 哪个编译器正确?

给出以下代码:

#include <string>
void foo()
{
  std::string s(std::move(""));
}
Run Code Online (Sandbox Code Playgroud)

这与apple clang(xcode 7)编译,而不是与visual studio 2015生成以下错误:

error C2440: 'return': cannot convert from 'const char [1]' to 'const char (&&)[1]'
note: You cannot bind an lvalue to an rvalue reference
main.cpp(4): note: see reference to function template instantiation 'const char (&&std::move<const char(&)[1]>(_Ty) noexcept)[1]' being compiled
    with
    [
        _Ty=const char (&)[1]
    ]
Run Code Online (Sandbox Code Playgroud)

暂时忽略移动是多余的,在这种情况下哪个标准库实现更正确?

我的感觉是该类型即""const char[1]如此std::move应该返回std::remove_reference<const char[1]&>::type&&这将是const char[1]&&.

在我看来,这应该衰败const char*.

或者我是否误解了这些规则?

c++ language-lawyer visual-studio-2015

27
推荐指数
1
解决办法
1116
查看次数

根据标准,哪些编译器有错误?

给出以下源代码:

#include <memory>
#include <iostream>

using namespace std;

struct concept
{
    virtual void perform() = 0;
};


struct model : concept, enable_shared_from_this<model>
{
    void perform() override {
        cout << "my pointer is " << shared_from_this().get() << endl;
    }
};

int main(int argc, const char * argv[])
{
    // shared_ptr<concept> concept_ptr = make_shared<model>();
    shared_ptr<concept> concept_ptr { new model };
    concept_ptr->perform();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译下gcc,此代码编译并将内部weak_ptr与地址相关联model.

clang代码下将无法编译(最后包含错误消息)

如果concept_ptrshared_ptr<concept> concept_ptr = make_shared<model>();它替换初始化,它将在两者上编译. …

c++ gcc clang c++11

26
推荐指数
1
解决办法
2094
查看次数

在实践中,unordered_map真的比地图快吗?

当然,unordered_map的查找性能平均是常量,并且映射的查找性能是O(logN).

但是当然为了在unordered_map中找到一个对象,我们必须:

  1. 哈希我们想要找到的密钥.
  2. equality_compare键与同一个桶中的每个键.

而在地图中,我们只需要将所搜索的密钥与log2(N)密钥进行比较,其中N是地图中的项目数.

我想知道真正的性能差异是什么,假设散列函数增加了开销并且equality_compare不比less_than比较便宜.

我写了一个测试,而不是用一个我可以自己回答的问题打扰社区.

我已经分享了下面的结果,以防其他人发现这个有趣或有用.

如果有人能够并且愿意添加更多信息,当然会邀请更多答案.

c++ performance dictionary unordered-map

14
推荐指数
2
解决办法
1万
查看次数

std :: vector :: reserve是否保证在这种情况下实现不会使迭代器失效?

这是一个打算:

a)接受int的向量

b)对于输入向量中的每个int,追加此int的逆

先决条件:无

postconditions:返回vector的size()是2*输入向量的大小.

请注意,矢量是就地修改的.

题:

在变换期间,此函数是否严格定义了与迭代器失效相关的行为?

奖金:

是否有更好/更简洁/更健壮的方式来编写它?

码:

std::vector<int> append_negatives(std::vector<int> v)
{
    v.reserve(v.size() * 2);
    std::transform(begin(v), end(v), 
                   back_inserter(v), 
                   [](auto&& x) { return -x; });
    return v;
}
Run Code Online (Sandbox Code Playgroud)

c++ stdvector language-lawyer c++11

14
推荐指数
1
解决办法
754
查看次数

用宏构造#include指令的路径

我想为宏程序动态创建的包含文件路径,用于程序的目标配置相关部分.

例如,我想构建一个可以像这样调用的宏:

#include TARGET_PATH_OF(header.h)
Run Code Online (Sandbox Code Playgroud)

这会扩展到这样的事情:

#include "corefoundation/header.h"
Run Code Online (Sandbox Code Playgroud)

当为OSX配置源(在本例中)时

到目前为止,所有尝试都失败了 我希望以前有人这样做过吗?

什么不起作用的例子:

#include <iostream>
#include <boost/preprocessor.hpp>

#define Dir directory/
#define File filename.h

#define MakePath(f) BOOST_PP_STRINGIZE(BOOST_PP_CAT(Dir,f))
#define MyPath MakePath(File)

using namespace std;

int main() {
    // this is a test - yes I know I could just concatenate strings here
    // but that is not the case for #include
    cout << MyPath << endl;
}
Run Code Online (Sandbox Code Playgroud)

错误:

./enableif.cpp:31:13: error: pasting formed '/filename', an invalid preprocessing token
    cout << MyPath << endl;
            ^
./enableif.cpp:26:16: …
Run Code Online (Sandbox Code Playgroud)

c++ macros include boost-preprocessor

12
推荐指数
1
解决办法
4137
查看次数

检查子对象的地址是否位于包含对象的边界内是否合法

2个问题:

  1. 以下代码是否已根据定义的行为很好地形成?

  2. 是否有任何可能的c ++实现可以断言?

代码(c ++ 11及更高版本):

#include <cassert>
#include <utility>
#include <ciso646>

template<class T> 
auto to_address(T* p) { return reinterpret_cast<unsigned char const*>(p); }

/// Test whether part is a sub-object of object
template<class Object, class Part>
bool is_within_object(Object& object, Part& part)
{
    auto first = to_address(std::addressof(object)),
                 last = first + sizeof(Object);

    auto p = to_address(std::addressof(part));

    return (first <= p) and (p < last);
}

struct X
{
    int a = 0;

    int& get_a() { return a; }
    int& get_b() …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer c++11 c++14 c++17

12
推荐指数
1
解决办法
616
查看次数

clang和gcc为相同的代码生成不同的逻辑.哪个是对的?

我发现gcc-8和clang-6产生的逻辑之间存在差异.

这发生在真正的代码库中,当使用clang开发时,我使用gcc进行部署.

请告知哪个编译器有错误,以便我可以适当地提交错误.

概要

A可以隐式转换为B A可以从A(复制/移动)和std::initializer_list<B>.

初始化时AA&&:

  • clang选择move-constructor
  • gcc选择initializer_list构造函数.

现场演示:https://coliru.stacked-crooked.com/a/bc50bd8f040d6476

MCVE

#include <initializer_list>
#include <utility>
#include <iostream>

struct thing;

struct thing_ref
{
    thing_ref(thing&& other) : ref_(other) {}
    thing_ref(thing& other) : ref_(other) {}

    thing& ref_;
};

struct thing
{
    thing() {}

    thing(std::initializer_list<thing_ref> things)
    {
        std::cout << "initializer_list path\n";
    }

    thing(thing&& other)
    {
        std::cout << "move path\n";
    }

    thing(thing const& other)
    {
        std::cout << "copy path\n"; …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer c++17

12
推荐指数
1
解决办法
408
查看次数

澄清P0137的细节

在下面的代码中,我在标准的单词(加上P0137的措辞)中对对象生命周期进行了细致的处理.

请注意,根据P0137,所有内存分配都是通过适当对齐的unsigned char类型存储.

还要注意,这Foo是一个POD,带有一个简单的构造函数.

问题:

A.如果我误解了标准,并且这里有任何UB,请将其指出(或者确认没有UB)

B. 鉴于构造是微不足道的,并且不执行实际的初始化,A,B,C,D,E,F的初始化是严格必要的.如果是这样,请说明标准的哪一部分在这方面与[object.lifetime]相矛盾或澄清.

码:

#include <memory>

// a POD with trivial constructor
struct Foo 
{
  int x;
};

struct destroy1
{
  void operator()(Foo* p)
  {
    // RAII to guarantee correct destruction order
    auto memory = std::unique_ptr<unsigned char[]>(reinterpret_cast<unsigned char*>(p));
    p->~Foo(); // A
  }
};
std::unique_ptr<Foo, destroy1> create1()
{
  // RAII to guarantee correct exception handling
  auto p = std::make_unique<unsigned char[]>(sizeof(Foo));
  auto pCandidate = reinterpret_cast<Foo*>(p.get());
  new (pCandidate) Foo(); // …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer c++17

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

哪个编译器(如果有的话)在参数包扩展中有错误?

在尝试以方便的方式访问元组作为容器时,我编写了一个测试程序.

在clang(3.9.1和apple clang)上,它按预期编译,产生预期的输出:

1.1
foo
2
Run Code Online (Sandbox Code Playgroud)

在gcc(5.4,6.3)上,它无法编译:

<source>: In lambda function:
<source>:14:61: error: parameter packs not expanded with '...':
             +[](F& f, Tuple& tuple) { f(std::get<Is>(tuple)); }...
                                                             ^
<source>:14:61: note:         'Is'
<source>: In function 'decltype(auto) notstd::make_callers_impl(std::index_sequence<Is ...>)':
<source>:14:64: error: expansion pattern '+<lambda>' contains no argument packs
             +[](F& f, Tuple& tuple) { f(std::get<Is>(tuple)); }...
                                                                ^~~
Compiler exited with result code 1
Run Code Online (Sandbox Code Playgroud)

问题:谁是对的?可以修复吗?

程序:

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

namespace notstd {

    template<class F, class Tuple, std::size_t...Is>
    auto make_callers_impl(std::index_sequence<Is...>) -> decltype(auto)
    { …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer variadic-templates c++14

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

如果已经隐式实例化了特化,则会隐式实例化它吗?

标题中的问题很清楚.更具体地说,请考虑以下示例:

#include <type_traits>

template <typename T>
struct is_complete_helper {
    template <typename U>
    static auto test(U*)  -> std::integral_constant<bool, sizeof(U) == sizeof(U)>;
    static auto test(...) -> std::false_type;
    using type = decltype(test((T*)0));
};

template <typename T>
struct is_complete : is_complete_helper<T>::type {};

// The above is an implementation of is_complete from https://stackoverflow.com/a/21121104/5376789

template<class T> class X;

static_assert(!is_complete<X<char>>::type{}); 
// X<char> should be implicitly instantiated here, an incomplete type

template<class T> class X {};

static_assert(!is_complete<X<char>>::type{}); // #1

X<char> ch; // #2
Run Code Online (Sandbox Code Playgroud)

此代码与GCC和Clang编译.

根据[temp.inst]/1 …

c++ templates language-lawyer template-instantiation implicit-instantiation

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