我使用cppreference阅读了C++中的枚举声明.
然后我制作了Enum类并检查它是否是类类型或不使用std::is_class.
#include <iostream>
enum class Enum
{
red = 1, blue, green
};
int main()
{
std::cout << std::boolalpha;
std::cout << std::is_class<Enum>::value << '\n';
}
Run Code Online (Sandbox Code Playgroud)
然后我在Linux平台上编译并运行G ++编译器,它打印出false值.
所以类型是enum不是?如果枚举是一个类类型,那么为什么我会得到假值?
我似乎找到了Clang和GCC不同意的东西.这是代码:
int main() {
if constexpr (2) {}
}
Run Code Online (Sandbox Code Playgroud)
这成功编译了GCC 7.4.0,但它与Clang 7.0.0失败并出现此错误消息:
test.cpp:3:17: error: constexpr if condition evaluates to 2, which cannot be narrowed to type 'bool'
[-Wc++11-narrowing]
if constexpr (2) {}
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
cppreference似乎没有提到"缩小",所以这看起来像一个Clang bug,但我不完全确定.如果这是任何一个编译器的错误,是否已报告?
我有幸遇到的最让我最喜爱/最邪恶的发明之一是constexpr计数器,也就是有状态的元编程.正如帖子中所提到的,它似乎在C++ 14下是合法的,我想知道C++ 17有什么变化吗?
以下是主要基于帖子的实现
template <int N>
struct flag
{
friend constexpr int adl_flag(flag<N>);
constexpr operator int() { return N; }
};
template <int N>
struct write
{
friend constexpr int adl_flag(flag<N>) { return N; }
static constexpr int value = N;
};
template <int N, int = adl_flag(flag<N>{})>
constexpr int read(int, flag<N>, int R = read(0, flag<N + 1>{}))
{
return R;
}
template <int N>
constexpr int read(float, flag<N>)
{
return N;
}
template <int N …Run Code Online (Sandbox Code Playgroud) 我发现gcc和clang允许decltype(auto)在非类型模板参数类型子句中使用.例如:
template <decltype(auto)>
struct X {};
int foo ;
int main() {
X<(foo)> x;
static_cast<void>(x);
}
Run Code Online (Sandbox Code Playgroud)
它是标准兼容功能还是一些gnu扩展?
C++ 1z将引入"constexpr if" - 如果将根据条件删除其中一个分支.似乎合理有用.
但是,没有constexpr关键字是不可能的?我认为在编译期间,编译器应该知道编译时间是否已知.如果是,即使是最基本的优化级别也应该删除不必要的分支.
例如(参见godbolt:https://godbolt.org/g/IpY5y5 ):
int test() {
const bool condition = true;
if (condition) {
return 0;
} else {
// optimized out even without "constexpr if"
return 1;
}
}
Run Code Online (Sandbox Code Playgroud)
Godbolt探险家表示,即使是带有-O0的gcc-4.4.7也没有编译"返回1",所以它实现了constexpr所承诺的.显然,当条件是constexpr函数的结果时,这样的旧编译器将无法这样做,但事实仍然存在:现代编译器知道条件是否为constexpr,并且不需要我明确地告诉它.
所以问题是:
为什么"constexpr if"需要"constexpr"?
在C++ 17中,实现一个overload(fs...)函数是很容易的,在fs...满足任意数量的参数的情况下FunctionObject,它返回一个行为类似于重载的新函数对象fs....例:
template <typename... Ts>
struct overloader : Ts...
{
template <typename... TArgs>
overloader(TArgs&&... xs) : Ts{forward<TArgs>(xs)}...
{
}
using Ts::operator()...;
};
template <typename... Ts>
auto overload(Ts&&... xs)
{
return overloader<decay_t<Ts>...>{forward<Ts>(xs)...};
}
int main()
{
auto o = overload([](char){ cout << "CHAR"; },
[](int) { cout << "INT"; });
o('a'); // prints "CHAR"
o(0); // prints "INT"
}
Run Code Online (Sandbox Code Playgroud)
由于上面的overloader继承Ts...,它需要复制或移动函数对象才能工作.我想要一些提供相同重载行为的东西,但只引用传递的函数对象.
我们称之为假设函数ref_overload(fs...).我的尝试正在使用std::reference_wrapper …
c++ overloading function-object template-meta-programming c++17
模板类std::iterator设置为在C++ 17中弃用.为什么这样?它是确保std::iterator_traits工作的一种方便方法,特别是如果您可以使用默认模板参数.在C++ 17中还有其他一些方法吗?
我有以下代码:
template <typename, typename>
struct AAA{};
template<typename ...Args>
void f(AAA<Args...> *) {}
int main() {
f<int, int>(nullptr);
}
Run Code Online (Sandbox Code Playgroud)
此代码导致编译错误.使用g++ -std=c++1z错误编译时显示如下:
prog.cc: In function 'int main()':
prog.cc:8:24: error: no matching function for call to 'f<int, int>(std::nullptr_t)'
f<int, int>(nullptr);
^
prog.cc:5:6: note: candidate: template<class ... Args> void f(AAA<Args ...>*)
void f(AAA<Args...> *) {}
^
prog.cc:5:6: note: template argument deduction/substitution failed:
prog.cc:8:24: note: mismatched types 'AAA<Args ...>*' and 'std::nullptr_t'
f<int, int>(nullptr);
Run Code Online (Sandbox Code Playgroud)
使用clang++ -std=c++1z错误是:
prog.cc:8:5: error: no matching function for …Run Code Online (Sandbox Code Playgroud) 假设我有一个向量:
std::vector<Foo> v;
Run Code Online (Sandbox Code Playgroud)
此向量已排序,因此相等的元素彼此相邻。
获得所有表示具有相等元素的范围的迭代器对的最佳方法是什么(使用标准库)?
while (v-is-not-processed) {
iterator b = <begin-of-next-range-of-equal-elements>;
iterator e = <end-of-next-range-of-equal-elements>;
for (iterator i=b; i!=e; ++i) {
// Do something with i
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何在上面的代码中获取b和的值e。
因此,例如,如果v包含以下数字:
index 0 1 2 3 4 5 6 7 8 9
value 2 2 2 4 6 6 7 7 7 8
Run Code Online (Sandbox Code Playgroud)
然后,我想在循环中具有b并e指向元素:
iteration b e
1st 0 3
2nd 3 4
3rd 4 6
4th 6 9
5th …Run Code Online (Sandbox Code Playgroud) c++ ×10
c++17 ×10
templates ×2
algorithm ×1
c++-concepts ×1
c++11 ×1
compiler-bug ×1
constexpr ×1
enums ×1
g++ ×1
if-constexpr ×1
overloading ×1