小编New*_*ine的帖子

用概念重载函数

(我正在学习概念和模板,所以如果我有什么不对的地方,请纠正我。)我有一个将概念作为参数的函数。我现在试图重载这个需要更具体概念的函数。那会做“更具体的事情”或调用不太具体的功能。

template<typename T>
concept Concept1 = ...;

template<typename T>
concept MoreSpecificConcept = ...;
    Concept1 <T> &&
    ...;

//...
void someFunc(const Concept1 auto& x)
{
  //do general stuff
}

void someFunc(const MoreSpecificConcept auto& x)
{
  if(...)
  {
    //do specific stuff
  }
  else
  {
    //do the more general thing:

    // Problem. Trying to call itself:
    someFunc(x);
  }
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以明确告诉编译器要调用哪个重载(例如someFunc<Concept1>(x)哪个不起作用),还是仅依赖于传递对象的类型?可以说我不能x转换为更通用的类型,并且更通用的函数/概念不知道这个更具体的函数/概念,所以他们不能用约束排除它。编辑:这些函数应该在同一个(全局)命名空间内。

c++ templates overloading c++-concepts c++20

8
推荐指数
1
解决办法
204
查看次数

C++ - 为什么聚合初始化不适用于模板结构

此代码可以工作,无需指定构造函数:

struct Foo
{
    int a;
    int b;
};

//...

    int a1, b1;

    Foo foo = {a1, b1};
Run Code Online (Sandbox Code Playgroud)

如果我将 Foo 作为模板,它就不起作用。

template<typename T1, typename T2>
struct Foo
{
    T1 a;
    T2 b;
};

//...

    int a1, b1;

    Foo foo = {a1, b1};
Run Code Online (Sandbox Code Playgroud)

它说推论失败/提供了 2 个参数,而预期有 1 个参数。如果我添加一个像这样的构造函数,Foo(T1, T2){}它就可以工作。我想,这种构造默认适用于结构。我错了什么?

编辑:我正在使用 Clang,它似乎不支持它。MSVC 和 GCC 都使用 c++20 编译器标志来编译它。

c++ templates initialization c++20 ctad

7
推荐指数
1
解决办法
436
查看次数

using 声明是否可以仅引用基于 const 限定的特定重载?

如果基类同时具有 const 和非 const 版本的函数,我可以通过 using 关键字在派生类中仅引用其中之一吗?

struct Base
{
protected:
    int x = 1;

    const int&  getX() const     {return x;}
    int&        getX()           {return x;}
};

struct Derived : public Base
{
public:
    using Base::getX; // Can we refer to the const or the non-const only?
};
Run Code Online (Sandbox Code Playgroud)

const getX()如果不是,在 Derived 中公开而不重复函数体的最简单方法是什么?

c++

6
推荐指数
1
解决办法
93
查看次数

C++ - 从类模板的继承构造函数的参数中推导类型

我有一个继承基类模板的构造函数的类模板。(对于c++20)有没有办法从基类的构造函数参数推导出派生类的模板参数?

如果我明确指定类型,那就有效。或者,如果我重新实现构造函数并调用基类的构造函数,这也可以工作,但是有没有办法做到这一点?

template<typename T>
struct CTestBase
{
    using Type = T;

    CTestBase() = default;
    CTestBase(T t){}
};

template<typename T>
struct CTestDer : public CTestBase<T>
{
    using CTestBase<T>::CTestBase;
};

void test()
{
    //CTestDer der(int{}); //ERROR
    CTestDer<int> der(int{}); //OK
}
Run Code Online (Sandbox Code Playgroud)

c++ class-template template-argument-deduction c++20

5
推荐指数
2
解决办法
428
查看次数

C++ - Bool 到 int 转换错误

我的代码中有一个函数,如下所示:

bool foo(ObjectType& object){

    //code

    const float result = object.float_data + object.bool_data * integer_data;

    //code

}
Run Code Online (Sandbox Code Playgroud)

在寻找错误时,我发现result有时会有不正确的值,原因是有时bool*integer被计算为255*integer而不是1*integer

C++ 说在整数上下文中bool会转换为零或一,所以我不明白这一点。我与bool代码中的其他部分相乘,效果很好。这也是随机的:有时会转换为1,有时会转换为255。调试器还分别显示true255。当这个错误发生时,它总是发生在该执行中。重新编译代码没有任何效果,它仍然随机发生。

c++ gcc c++17

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

c++20 中是否应该抑制有关缺少类型名的警告?

警告消息如:

missing 'typename' prior to dependent type name ... [-Wtypename-missing]

template argument for template type parameter must be a type; omitted 'typename' is a Microsoft extension [-Wmicrosoft-template]

如果我理解正确的话,c++20 放松了对typename. 这是否意味着这些警告已经过时了?typename或者我应该在出现警告时添加(烦人的) ?(我使用的是 Visual Studio / Clang12 / std=C++20。)

c++ compiler-warnings c++20

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

无法使用嵌套类的 constexpr 函数

struct Foo
{
    struct Bar
    {
        int data = 0;

        //constexpr Bar() = default; // Doesn't work either
        constexpr Bar() : data(0) {}
    };

    static constexpr Bar bar = {}; // ERROR
    //static constexpr Bar bar = {0}; // Works if the ctor from Bar is removed
};
Run Code Online (Sandbox Code Playgroud)

Clang 和 GCC(使用 std=c++20)说我尝试使用的构造函数未定义。但如果我改为 ,它就会constexpr起作用inline。我想了解使用“contexpr”有什么问题。

c++ static-members constexpr c++20

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

C++ - 是否将 const 幻数放入命名空间

我总是习惯#define在 cpp 文件开头的某个地方定义幻数。我想把它改成const数字。(全局变量在 cpp 文件中声明/定义。)这是个好主意吗?我应该将它们放入匿名命名空间吗?我从不#include在任何地方使用 cpp 文件。

c++ namespaces constants magic-numbers

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