小编Ome*_*waz的帖子

接受类对象的模板函数可以使用其构造函数参数实例化该对象吗?

假设我有一个采用类对象的模板函数:

template<class T>
void Foo(T obj);
Run Code Online (Sandbox Code Playgroud)

和一个类定义如下:

class Bar 
{
public:
    Bar(int a, bool b): _a(a), _b(b) {}
private:
    int _a;
    bool _b;
};
Run Code Online (Sandbox Code Playgroud)

有没有办法让下面的代码编译?

Foo<Bar>(5,false);
Foo<Bar>({5,false}); // i know this works, just wondering if i can remove the brackets somehow.
Run Code Online (Sandbox Code Playgroud)

c++ initializer-list implicit-conversion

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

在 C++ 中,如何强制模板参数为作用域枚举值类型?

我有一个类模板,我想编写如下:

template </*what to put here?*/ T>
Class Bar {};
Run Code Online (Sandbox Code Playgroud)

我想强制 T 只能是作用域枚举中的值。我使用了此处is_scoped_enum提供的类型检查,但是我能想到的最好的办法就是更改为如下所示:Bar

template <typename T>
concept ScopeEnum = is_scoped_enum<T>::value;

template<ScopeEnum SE, SE se>
class Bar {};
Run Code Online (Sandbox Code Playgroud)

我怎样才能实现它并Bar保持预期的效果?

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

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