标签: constexpr

constexpr和bizzare错误

我有:

constexpr bool is_concurrency_selected()const
    {
        return ConcurrentGBx->isChecked();//GBx is a groupbox with checkbox
    }
Run Code Online (Sandbox Code Playgroud)

我收到错误:

C:\...\Options_Dialog.hpp:129: error: enclosing class of 'bool Options_Dialog::is_concurrency_selected() const' is not a literal type
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

c++ constexpr c++11

3
推荐指数
1
解决办法
2782
查看次数

为什么要使用constexpr

我们来看一个简单的SFINAE模板示例

#include <iostream>

template <typename T>
struct has_typedef_foobar {
    // Types "yes" and "no" are guaranteed to have different sizes,
    // specifically sizeof(yes) == 1 and sizeof(no) == 2.
    typedef char yes[1];
    typedef char no[2];

    template <typename C>
    static yes& test(typename C::foobar*);

    template <typename>
    static no& test(...);

    // If the "sizeof" of the result of calling test<T>(0) would be equal to sizeof(yes),
    // the first overload worked and T has a nested type named foobar.
    static const bool value …
Run Code Online (Sandbox Code Playgroud)

c++ sfinae constexpr c++11 c++14

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

C++ - 检查所有模板参数是否为2的幂

我试图找到一种简单的方法来检查作为模板参数传递的参数是否都是2的幂.我在网站上找到了一个bithack,我有这个:

constexpr bool isPowerOf2(size_t value){
return !(value == 0) && !(value & (value - 1));
}
Run Code Online (Sandbox Code Playgroud)

这适用于单个值,但将此应用于多个参数看起来很难看.

static_assert(isPowerOf2(Arg1), "Argument must be a power of 2");
static_assert(isPowerOf2(Arg2), "Argument must be a power of 2");
static_assert(isPowerOf2(Arg3), "Argument must be a power of 2");
Run Code Online (Sandbox Code Playgroud)

如果我能让它看起来像arePowersOf2(Arg1,Arg2,Arg3)会更好,但是我并不是真的在模板魔术方面先进.所以我的问题是:有一种简单的方法吗?我更喜欢constexpr C++ 11解决方案.

templates variadic constexpr c++11

3
推荐指数
1
解决办法
390
查看次数

为什么我的isVector函数不返回true?

基于这个问题,我尝试了一个is_vector特点:

#include <iostream>
#include <vector>

using namespace std;

template<typename T>
struct is_vector { 
        constexpr static bool value = false;
};

template<typename T>
struct is_vector<std::vector<T>> { 
        constexpr static bool value = true;
};

int main() { 
    int A;
    vector<int> B;

    cout << "A: " << is_vector<decltype(A)>::value << endl;
    cout << "B: " << is_vector<decltype(B)>::value << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

A: 0
B: 1
Run Code Online (Sandbox Code Playgroud)

这按预期工作.然而,当我试图把这个小的辅助函数,is_vector返回falseB:

template<typename T>
constexpr bool isVector(const T& …
Run Code Online (Sandbox Code Playgroud)

c++ decltype constexpr

3
推荐指数
1
解决办法
80
查看次数

C中的Const表达式 - 如何散列字符串?

我正在创建一个内核模块.在这个模块中,我需要检查一些预定义字符串的输入.在C++中,可以创建一个constexpr函数,用于在编译时计算哈希值.我正在寻找一种方法来做到这一点.

一些使用Jenkins哈希函数的伪代码:

u32 hash(const char *key)
{
    u32 hash, i;
    size_t len;

    len = strlen(key);
    for(hash = i = 0; i < len; ++i)
    {
        hash += key[i];
        hash += (hash << 10);
        hash ^= (hash >> 6);
    }
    hash += (hash << 3);
    hash ^= (hash >> 11);
    hash += (hash << 15);
    return hash;
}


const u32 myStringList [] = {
    hash("hello"),
    hash("this is a text"),
    hash("good morning")
};

int findString(const char * searchStr) {
    u32 …
Run Code Online (Sandbox Code Playgroud)

c hash constexpr

3
推荐指数
1
解决办法
117
查看次数

在hana教程中使用nonconstexpr对象的static_assert

阅读hana的教程,我想知道如何static_assert按预期工作:

template <typename Any>
auto switch_(Any& a) {
  return [&a](auto ...cases_) {
    auto cases = hana::make_tuple(cases_...);

    auto default_ = hana::find_if(cases, [](auto const& c) {
      return hana::first(c) == hana::type_c<default_t>;
    });

    static_assert(default_ != hana::nothing,
      "switch is missing a default_ case");

    // ...
  };
}
Run Code Online (Sandbox Code Playgroud)

文档明确声明default_不是constexpr对象,因此,即使operator!=这些类型的重载是constexpr函数,表达式default_ != hana::nothing也不能是常量表达式,因为它的一个参数不是.

教程说:

请注意我们如何在没有任何内容的情况下使用static_assert进行比较的结果,即使它default_是一个非constexpr对象?大胆地说,Hana确保在运行时没有丢失编译时已知的信息,这显然是案例存在的 default_情况.

教程在该段落中引用了什么,或者该表达式如何工作?

c++ static-assert constexpr

3
推荐指数
1
解决办法
38
查看次数

如果基类构造函数不是constexpr,我可以创建派生类constexpr的构造函数吗?

我试图从"设计模式"编译一个例子,我面临以下问题:

我有一个基类MapSite:

class MapSite{
public:
    MapSite();
    virtual ~MapSite(); 
    virtual void Enter() = 0;
};
Run Code Online (Sandbox Code Playgroud)

和派生类房间:

class Room : public MapSite final{

private:
    unsigned int room_number;

public:
    Room(unsigned int rn) : room_number(rn) {};

    virtual void Enter() override;
};
Run Code Online (Sandbox Code Playgroud)

从另一个类我想调用该函数

virtual std::unique_ptr<Room> MakeRoom(unsigned int n) {return make_unique<Room(n)>();} 
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我收到以下错误:

error: temporary of non-literal type ‘Room’ in a constant expression
  virtual std::unique_ptr<Room> MakeRoom(unsigned int n) {return unique::make_unique<Room(n)>();}
Run Code Online (Sandbox Code Playgroud)

所以我认为问题可能是构造函数必须constexpr从另一个函数调用Room的构造函数,但是将构造函数设置为:

constexpr Room(unsigned int rn) : room_number(rn) {};
Run Code Online (Sandbox Code Playgroud)

会产生这个错误:

error: call to non-constexpr function ‘MapSite::MapSite()’ …
Run Code Online (Sandbox Code Playgroud)

c++ constructor constexpr c++11 c++14

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

c ++ 11中的constexpr概念

我将通过C++中的constexpr概念.我的理解是constexpr在编译时得到评估.在这里,我找到了一个示例,他们有以下代码段.

int z[30];

constexpr auto e2 = &z[20] - &z[3];
Run Code Online (Sandbox Code Playgroud)

他们正在计算编译时地址之间的差异.当我们在编译时不知道地址的实际值时,如何在编译时评估它?

c++ constexpr c++11

3
推荐指数
1
解决办法
168
查看次数

C ++ 20 constexpr容器将如何工作?

作为constexpr std::stringconstexpr std::vector已被接受为C ++ 20,如何将这些被使用?链接的文件在细节上很短。我们是否需要指定特殊的constexpr分配器,使编译时字符串/向量与其常规等效项不兼容?

c++ containers stl constexpr c++20

3
推荐指数
1
解决办法
251
查看次数

Constexpr变量不是编译时间值吗?

根据https://docs.microsoft.com/zh-cn/cpp/cpp/constexpr-cpp?view=vs-2019

“ constexpr表示该值或返回值是恒定的,并且,如果可能,在编译时进行计算。”

此外,constexpr和静态constexpr全局变量之间的区别

“在变量声明中,constexpr隐含const,并且默认情况下,名称空间范围内的const变量具有内部链接(因此,添加static不会更改任何内容)。”

我认为这constexpr意味着const隐含static

但是,这个问题的答案使我感到困惑:什么时候以及为什么在constexpr中使用static?

它指出

  1. “ constexpr变量不是编译时值”
  2. “看来,在某些特殊情况下,我们可以受益于静态constexpr变量的静态存储持续时间。”

我有什么误会?

c++ constexpr

3
推荐指数
1
解决办法
92
查看次数