鉴于可用性make_unique
和make_shared
自动删除unique_ptr
以及shared_ptr
析构函数,在C++ 14中使用new
和使用的情况(除了支持遗留代码之外)是delete
什么?
以下文章是我为模板参数包找到的第一个提案.
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)
那么这些运营商在哪里?如果没有,他们的替代品是什么?
在回答这个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
?
以下代码不会在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) 作为在讨论这个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涵盖了这一点
我想按字典顺序对大量整数(比如说1个元素)进行排序.
例:
input [] = { 100, 21 , 22 , 99 , 1 , 927 }
sorted[] = { 1 , 100, 21 , 22 , 927, 99 }
Run Code Online (Sandbox Code Playgroud)
我用最简单的方法完成了它:
std:sort
与strcmp
作为比较功能有没有比这更好的方法?
我是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++中使用枚举作为模板(类型)参数有任何限制/问题吗?
例:
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个),但这个在我看来非常奇怪.
任何建议如何弄清楚它是否是编译器?
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) 我有一个程序如下:
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中的副本捕获的变量.
我的问题是:如果我们选择使用副本(使用"="或值名称),是否不能为此值分配新值或更改?