我从事的项目具有以下内容:
enum SomeType {
value_100 = 100,
reserved_101 = 101,
value_102 = 102
};
Run Code Online (Sandbox Code Playgroud)
这些“保留的”标识符的存在并不是要改变的权力。
我想以某种方式装饰它们,类似于
[[maybe_unused]]
,所以我可以打开-Werror=switch
这是一个巨大的(而且很旧的)代码库,重构不使用开关将是一项艰巨的任务,而不是现在的事。
有什么方法可以选择性地使有关gcc中未处理的枚举值的警告静音?请注意,我仍然不希望对枚举值的警告进行修饰。使用gcc扩展名是可以的。
在 CppCon 的一次演讲中(https://www.youtube.com/watch?v=80BZxujhY38 5:00)Herb Sutter暗指在defun defun 3
某种程度上是一个问题。在我用谷歌搜索之后,我仍然不清楚为什么。有人可以详细说明吗?
以下不会编译(尝试过clang和gcc)
#include <vector>
struct Foo
{
Foo(int a=0) : m_a(a) {}
Foo(const Foo& f) = delete;
// Foo(Foo&& f) = default;
private:
int m_a;
};
int main()
{
std::vector<Foo> foovec;
foovec.emplace_back(44); // might resize, so might move
}
Run Code Online (Sandbox Code Playgroud)
但是如果我不删除复制构造函数,或者如果我默认移动构造函数,
它将起作用.因此,没有删除拷贝构造函数抑制移动构造函数,
并且什么是背后的理性?
当我注意到你不需要以下内容时,我试图为SFINAE做一个激励性的例子:
#include <iostream>
using namespace std;
template<typename T>
struct Foo
{
void add(int count, const T& val) // add "count" number of val's
{
cout << "count, val" << endl;
}
template<typename It>
void add(It beg, It end) // add items [beg,end)
{
cout << "beg, end" << endl;
}
};
int main()
{
int a=1;
int xx[] = {1,2,3};
Foo<int> foo;
foo.add(xx, xx+3);
foo.add(2, a);
}
Run Code Online (Sandbox Code Playgroud)
这编译,运行和打印:
求,结束
计数,val
我不明白为什么第二次打电话add
不含糊.
我想知道如何在函数式编程语言中这样做.也许F#或Haskell.
有人可以告诉我一个例子而不使用任何函数调用除了find
和rfind
?
此函数使用i
num slash
((向后<0)查找下一个斜杠.
size_t findSlash(const char *sz, size_t i)
{
std::string s = sz;
size_t a, b, c, n, ai = abs(i), pos=0;
for (n=0; n<ai; n++)
{
if (i<0)
{
a = s.rfind("\\", pos);
b = s.rfind("/", pos);
}
else
{
a = s.find("\\", pos);
b = s.find("/", pos);
}
if (a==-1u)
{
if (b==-1u)
return pos;
c = b;
}
else if (b==-1u)
c = a;
else
c = min(a, …
Run Code Online (Sandbox Code Playgroud) 我无法使用g ++或clang编译以下代码片段
void foo(vector<bool>& p)
{
for( auto& b : p )
b=true;
}
Run Code Online (Sandbox Code Playgroud)
我知道有/曾经是vector<bool>
专业化.
这是一个已知的错误?或者标准是否例外?
还是我错过了一些简单的东西?
g ++给了我以下内容:
error: invalid initialization of non-const reference of type std::_Bit_reference
from an rvalue of type std::_Bit_iterator::reference {aka std::_Bit_reference}
Run Code Online (Sandbox Code Playgroud)
clang给出:
error: non-const lvalue reference to type 'std::_Bit_reference' cannot bind to a
temporary of type 'reference' (aka 'std::_Bit_reference')
Run Code Online (Sandbox Code Playgroud) make_unique
如果设计如下,则不会更有用:
template< typename T, typename U=T, typename ...ArgT>
unique_ptr<T> make_unique( ArgT ...args )
{
return unique_ptr<T>( new U(args...) );
// maybe parameters should be forwarded, not really important for this question
}
Run Code Online (Sandbox Code Playgroud)
这样你就可以用它来创建派生对象了吗?
谁知道不这样做的理性背后?