小编dyp*_*dyp的帖子

新的和删除在C++ 14中仍然有用吗?

鉴于可用性make_uniquemake_shared自动删除unique_ptr以及shared_ptr析构函数,在C++ 14中使用new和使用的情况(除了支持遗留代码之外)是delete什么?

c++ new-operator dynamic-memory-allocation c++11 c++14

59
推荐指数
2
解决办法
3809
查看次数

模板参数包访问第N个类型和第N个元素

以下文章是我为模板参数包找到的第一个提案.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1603.pdf

在第16页,它讨论了引入两个新的运算符[]和<>来访问参数包元素和参数包类型.

The suggested syntax for such an operator involves two new operators: .[] to access values and .<> to access types. For instance:

template<int N, typename Tuple> struct tuple_element;
template<int N, ... Elements>
struct tuple_element<tuple<Elements...> >
{
    typedef Elements.<N> type;
};

template<int N, ... Elements>
Elements.<N>& get(tuple<Elements...>& t)
{ return t.[N]; }

template<int N, ... Elements>
const Elements.<N>& get(const tuple<Elements...>& t)
{ return t.[N]; }
Run Code Online (Sandbox Code Playgroud)

那么这些运营商在哪里?如果没有,他们的替代品是什么?

c++ variadic-templates c++11

37
推荐指数
4
解决办法
2万
查看次数

指针作为非类型模板参数

在回答这个SO问题时,我在标准(已经是C++ 03,仍在C++ 11中)中发现,如果它们是形式的& id-expression(除了一些例外),你只能使用地址作为非类型模板参数.

但我无法回答为什么会这样.

14.3.2模板非类型参数[temp.arg.nontype]

非类型非模板模板参数的模板参数应为以下之一:

[...]

- 一个常量表达式(5.19),用于指定具有静态存储的对象的地址>持续时间和外部或内部链接或具有外部或内部链接的函数,包括函数模板和函数模板-id,但不包括非静态类成员,表示(忽略括号)as&id-expression,除非如果名称引用函数或数组,则可以省略&,如果相应的template-parameter是引用,则省略; [...]

(n3485,强调我的)

例:

using TFoobar = int (*)();
template < TFoobar tp > struct foo_struct{};

int foobar() { return 42; }
constexpr TFoobar pFoobar = &foobar;

foo_struct < &foobar > o0; // fine
foo_struct < pFoobar > o1; // ill-formed
Run Code Online (Sandbox Code Playgroud)

我想这与翻译阶段有关,即编译器对地址知之甚少.然而,为什么不允许这样做?它不应该是可能的编译器使用类似宏替换的东西来代替pFoobar&foobar

c++ templates

23
推荐指数
1
解决办法
1324
查看次数

为什么我需要在map :: emplace中使用piecewise_construct用于不可复制对象的单个arg构造函数?

以下代码不会在gcc 4.8.2上编译.问题是此代码将尝试复制构造,std::pair<int, A>这是由于struct A缺少复制和移动构造函数而无法发生的.

gcc在这里失败了还是我错过了什么?

#include <map>
struct A
{
  int bla;
  A(int blub):bla(blub){}
  A(A&&) = delete;
  A(const A&) = delete;
  A& operator=(A&&) = delete;
  A& operator=(const A&) = delete;
};
int main()
{
  std::map<int, A> map;
  map.emplace(1, 2); // doesn't work
  map.emplace(std::piecewise_construct,
          std::forward_as_tuple(1),
          std::forward_as_tuple(2)
  ); // works like a charm
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ map c++11 std-pair emplace

22
推荐指数
1
解决办法
6260
查看次数

声明constexpr initializer_list对象是否合法?

作为在讨论这个SO问题时提出的一个问题:

申报对象是否合法,可能是N3471constexpr std::initializer_list?例:

constexpr std::initializer_list<int> my_list{};
Run Code Online (Sandbox Code Playgroud)

为什么我认为它可能不合法:initializer_list必须是字面类型; 但有没有保证它是文字类型?

来自N3485的引文.

[dcl.constexpr]/9:

对象声明中使用的constexpr说明符将对象声明为const.这样的对象应具有文字类型并应初始化.

文字类型要求,[basic.types]/10,子项目类类型:

  • 具有以下所有属性的类类型(第9节):
    • 它有一个简单的析构函数,
    • 非静态数据成员(如果有)的brace-or-equal-initializers中的每个构造函数调用和完全表达式都是一个常量表达式(5.19),
    • 它是一个聚合类型(8.5.1)或者至少有一个constexpr构造函数或构造函数模板,它不是复制或移动构造函数,并且
    • 它的所有非静态数据成员和基类都是非易失性文字类型.

奖励积分;)用于回答if

constexpr std::initializer_list<int> my_list = {1,2,3,4,5};
Run Code Online (Sandbox Code Playgroud)

是合法的(有参考).虽然我认为上述+ [dcl.init.list]/5涵盖了这一点

c++ initializer-list language-lawyer constexpr c++11

20
推荐指数
1
解决办法
6173
查看次数

按字典顺序排序C++的整数数组

我想按字典顺序对大量整数(比如说1个元素)进行排序.

例:

input [] = { 100, 21 , 22 , 99 , 1  , 927 }
sorted[] = { 1  , 100, 21 , 22 , 927, 99  }
Run Code Online (Sandbox Code Playgroud)

我用最简单的方法完成了它:

  • 将所有数字转换为字符串(非常昂贵,因为它将占用大量内存)
  • 使用std:sortstrcmp作为比较功能
  • 将字符串转换回整数

有没有比这更好的方法?

c++ arrays sorting lexicographic

18
推荐指数
4
解决办法
6758
查看次数

C++ 11线程简单的例子

我是c ++的新手,我正在研究一些c ++跨平台线程教程.我正在研究这个问题:http://solarianprogrammer.com/2011/12/16/cpp-11-thread-tutorial/

并试图执行以下代码:

#include <iostream>
#include <thread>

static const int num_threads = 10;

//This function will be called from a thread

void call_from_thread(int tid) {
    std::cout << "Launched by thread " << tid << std::endl;
}

int main() {
    std::thread t[num_threads];

    //Launch a group of threads
    for (int i = 0; i < num_threads; ++i) {
        t[i] = std::thread(call_from_thread, i);
    }

    std::cout << "Launched from the main\n";

    //Join the threads with the main thread
    for (int …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading iostream c++11

17
推荐指数
2
解决办法
4万
查看次数

在C++中使用enum作为模板类型参数

在C++中使用枚举作为模板(类型)参数有任何限制/问题吗?

例:

enum MyEnum
{
    A, B, C, D, E
};

template <typename _t>
class MyTemplate
{
public:
   _t value;

   void func(const _t& param) { /* .... */ }
};

// ....

MyTemplate<MyEnum> MyInstance;
Run Code Online (Sandbox Code Playgroud)

我在Win32/x86上通过VS 2008(SP1)使用MSVC++的实际问题是与使用枚举作为模板参数的类相关联的几个编译错误(=编译器报告的错误).遗憾的是,我的项目变得有点复杂(您可以将其视为设计错误:P),引发,嵌套甚至专门针对具有枚举模板参数的类的模板类会引发这些错误.

尝试构建时,编译器会在只有注释的行中报告许多错误/无用的错误,例如"C2059:语法错误:'public'".其中许多我可以通过替换类似于示例中的方法来修复const _t¶m by _t(即复制参数),但我也无法解决所有这些错误,也不知道为什么这个"帮助" .**我知道,上面的简单例子编译没有错误.

使用int而不是enum,我的项目编译没有错误.

提前感谢任何提示或提示!


编辑:

毕竟,我认真考虑这是一个编译器错误.当我尝试使用简化代码重现错误时,我只在50%的所有"构建"中获得它们,而不是非常确定性:
例如,尝试编译,并报告了这些错误.重建 - 没有变化.删除评论,构建 - 没有变化.重建 - 然后:没有错误,编译好.

我已经遇到了一些编译器错误(我估计在20k行代码中有2或3个),但这个在我看来非常奇怪.
任何建议如何弄清楚它是否是编译器?

c++ enums templates visual-c++-2008

16
推荐指数
2
解决办法
3万
查看次数

vector :: insert是否只允许保留一次并避免进一步的容量检查?

vector::insert(dst_iterator, src_begin, src_end)(插入范围)可以针对随机访问迭代器进行优化,以首先保留所需的容量src_end - src_begin,然后执行复制.

我的主要问题是:标准是否也允许vector::insert避免对每个复制元素进行容量检查?(即不在push_back每个要插入的元素上使用或类似)

我将把这个容量检查称为"优化insert".


可能出现的问题:我可以想象一个在解除引用时带有副作用迭代器:

注意:标准保证传递给它的迭代器insert将被解除引用一次(参见问题结尾).

#include <vector>
#include <iterator>
#include <iostream>

template < typename T >
struct evil_iterator : std::iterator < std::random_access_iterator_tag, T >
{
    using base = std::iterator < std::random_access_iterator_tag, T >;

    std::vector<T>* evil_feedback;
    typename std::vector<T>::iterator innocent_iterator;

    evil_iterator( std::vector<T>* c,
                   typename std::vector<T>::iterator i )
        : evil_feedback{c}
        , innocent_iterator{i}
    {}

    void do_evil()
    {
        std::cout << "trying to do evil; …
Run Code Online (Sandbox Code Playgroud)

c++ iterator vector language-lawyer

15
推荐指数
1
解决办法
947
查看次数

捕获列表中的C++ lambda拷贝值

我有一个程序如下:

int main()
{
    int val = 4;
    auto add = [val](int a)->int{
        val += 2;
        return a+val;
    };
    cout << add(3) << endl;
    cout << val << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Xcode中存在编译错误:无法分配给非可变lambda中的副本捕获的变量.

我的问题是:如果我们选择使用副本(使用"="或值名称),是否不能为此值分配新值或更改?

c++ lambda c++11

13
推荐指数
3
解决办法
6654
查看次数