标签: c++17

"enum class"是C++中的类类型吗?

我使用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不是?如果枚举是一个类类型,那么为什么我会得到值?

c++ enums c++11 c++17

41
推荐指数
2
解决办法
5750
查看次数

Constexpr如果是非bool条件

我似乎找到了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,但我不完全确定.如果这是任何一个编译器的错误,是否已报告?

c++ language-lawyer compiler-bug implicit-conversion c++17

41
推荐指数
2
解决办法
1688
查看次数

有状态的元编程是否形成不良(尚)?

我有幸遇到的最让我最喜爱/最邪恶的发明之一是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)

c++ metaprogramming language-lawyer c++17

40
推荐指数
1
解决办法
2308
查看次数

c ++ 17中的非类型模板参数可以是decltype(auto)吗?

我发现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)

[现场演示gcc] [现场演示铿锵]

它是标准兼容功能还是一些gnu扩展?

c++ templates language-lawyer c++17

40
推荐指数
1
解决办法
1151
查看次数

如何使用C++概念("concepts lite")支持构建gcc?

C++标准委员会正在研究概念扩展的TS(技术规范):"Programming Languages - C++ Extensions for Concepts".N4377是本文档的最新版本.为了包含在C++标准中,要求实现功能,理想情况是可公开访问的系统.

我知道概念-gcc,但上面的概念提议(通俗地称为Concepts Lite)是不同的.我听说有一个概念的分支,我已经尝试了origin/asutton/c++-conceptsGCCgit镜,但没有编制.如何构建和使用上述[草案] TS中指定的gcc支持概念版本?

c++ g++ generic-programming c++-concepts c++17

39
推荐指数
2
解决办法
7364
查看次数

"constexpr if"vs"if"with optimizations - 为什么需要"constexpr"?

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++ constexpr c++17 if-constexpr

39
推荐指数
1
解决办法
1455
查看次数

通过引用重载多个函数对象

在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)

wandbox上的实例


由于上面的overloader继承Ts...,它需要复制或移动函数对象才能工作.我想要一些提供相同重载行为的东西,但只引用传递的函数对象.

我们称之为假设函数ref_overload(fs...).我的尝试正在使用std::reference_wrapper …

c++ overloading function-object template-meta-programming c++17

39
推荐指数
2
解决办法
1786
查看次数

为什么不推荐使用std :: iterator?

模板类std::iterator设置为在C++ 17中弃用.为什么这样?它是确保std::iterator_traits工作的一种方便方法,特别是如果您可以使用默认模板参数.在C++ 17中还有其他一些方法吗?

c++ c++17

39
推荐指数
2
解决办法
6165
查看次数

为什么函数参数类型中使用的模板参数包作为其模板参数列表无法显式指定

我有以下代码:

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)

c++ templates c++17

39
推荐指数
4
解决办法
1641
查看次数

如何使用标准库迭代相等的值?

假设我有一个向量:

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)

然后,我想在循环中具有be指向元素:

 iteration  b  e
 1st        0  3
 2nd        3  4
 3rd        4  6
 4th        6  9
 5th …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm c++-standard-library iterator-range c++17

39
推荐指数
5
解决办法
1827
查看次数