我正在尝试创建一个函数模板,它将函数模板作为模板参数,然后在使用传入的普通函数参数调用时返回该函数的结果。它将像这样使用:
auto fooPtr = func<std::make_unique, Foo>(...);
Run Code Online (Sandbox Code Playgroud)
该函数的要点是即使在让另一个函数执行实例的构造时也允许模板类型推导。我已经在代码中的很多地方手动执行此操作,如下所示:
auto fooPtr = std::make_unique<decltype(Foo{...})>(...);
Run Code Online (Sandbox Code Playgroud)
我从我发布的问题的答案中得到了辅助函数的想法。他建议为特定类型制作一个函数,但我想要一个可用于任何类型的函数。
到目前为止,这是我想到的:
template
<auto F, template<typename U> class T, typename... Args>
std::result_of_t<decltype(F)>
func(Args&&... args, std::enable_if_t<std::is_invocable_v<decltype(F), Args...>>* = nullptr)
{
return F<decltype(T{std::forward<Args>(args)...})>(std::forward<Args>(args)...);
}
Run Code Online (Sandbox Code Playgroud)
但我无法让它发挥作用。
我走在正确的轨道上吗?我想做的事情可能吗?
我有两个枚举:
enum class YellowFruits
{
Banana,
Lemon
};
enum class RedFruits
{
Apple,
Peach
};
Run Code Online (Sandbox Code Playgroud)
我想将这两者合并为一个枚举:
enum class Fruits
{
//YellowFruits and RedFruits
};
Run Code Online (Sandbox Code Playgroud)
所以它的工作原理是这样的:
enum class Fruits
{
Banana,
Lemon,
Apple,
Peach
};
Run Code Online (Sandbox Code Playgroud)
但我找不到办法做到这一点。在寻找解决方案时,我找到了这个答案:Combine enums c++。但答案中的解决方案对我不起作用。我希望新的枚举能够正常工作,就像我使用它应从其他枚举获取的值创建它一样。有没有办法做到这一点?
考虑这个类和可变成员函数:
class foo
{
public:
template<class C, class... Cs>
void add(C&& c, Cs&&... cs)
{
...
add(cs...);
}
private:
void add(){ }
};
Run Code Online (Sandbox Code Playgroud)
有没有办法将终止add
递归的空重载放置在另一个范围内?请忽略这通常是否是一个好主意,我严格询问如何才能做到这一点(如果有的话)。我想将它放在一个impl
命名空间中,如下所示:
namespace impl
{
void add() {}
}
class foo
{
public:
template<class C, class... Cs>
void add(C&& c, Cs&&... cs)
{
...
using namespace impl;
add(cs...);
}
};
Run Code Online (Sandbox Code Playgroud)
但上面的代码在启动时不起作用。add
当参数包为空时,编译器抱怨找不到匹配的函数调用。
有没有办法使用一些范围黑客来实现这一目标?
我有六个不同的类,它们都派生自名为“Piece”的抽象基类。
当循环遍历指向 Piece 的指针向量时:
std::vector<shared_ptr<Piece>>
Run Code Online (Sandbox Code Playgroud)
有没有一种有效的方法来获取每个指针指向的派生类的类型?
显然我可以做这样的事情:
class Piece
{
public:
virtual std::string getType() = 0;
}
class Rook : public Piece
{
public:
std::string getType() override
{
return "Rook";
}
}
class Pawn : public Piece
{
public:
std::string getType() override
{
return "Pawn";
}
}
for (std::shared_ptr<Piece> p: std::vector<std::shared_ptr<Piece>>)
{
if (p->getType() == "Rook")
{
//Do something
}
else if(p->getType() == "Pawn")
{
//Do something else
}
}
Run Code Online (Sandbox Code Playgroud)
但似乎有更好的方法来做到这一点?
在cppreference-page上有std::declval
以下内容:
返回值
无法调用,因此永远不会返回值。
这是什么意思?我们在使用的时候肯定会调用它吗?
struct Foo { int func(){} };
decltype(std::declval<Foo>().func()) integer;
/* ^
| */
Run Code Online (Sandbox Code Playgroud) 我有一个包含内部类的类,它看起来有点像这样:
class Foo
{
class Bar
{
public:
void update(const Foo& f)
{
//Updates using public getters of Foo
}
};
public:
void doStuff() const
{
b_.update(*this);
}
private:
Bar b_;
};
Run Code Online (Sandbox Code Playgroud)
该线路b_.update(*this)
目前给我错误cannot convert 'this' pointer from 'const Foo::Bar' to 'Foo::Bar &'
这是为什么?该update
方法的参数是 type const Foo&
,这意味着它不可能修改Foo
对象,如果它愿意的话。这不应该满足 const 方法doStuff
吗?
我试过在谷歌上搜索一个有同样问题的人一个小时,但我似乎无法正确地用词来找到类似的问题。
我正在尝试做的事情是可能的,还是我需要改变我的设计?
我想检查给定的类是否只有以下内容:
除了用户定义的构造函数和析构函数之外,此类型(至少在视觉上声明方面)与 POD 类型相同。我试图为这种类型找到一个术语,但我认为它不存在。
有没有办法使用 SFINAE 黑客来检查这一点?
这个类包含一个联合:
struct foo
{
union
{
std::vector<int> vec;
int i;
};
};
Run Code Online (Sandbox Code Playgroud)
无法实例化。如果我尝试,编译器会抛出一个错误,指出'foo::foo(void)': attempting to reference a deleted function
. 为了让它工作,我必须向联合添加一个空的构造函数和析构函数,如下所示:
struct foo
{
union U
{
U() {}
~U() {}
std::vector<int> vec;
int i;
} u_;
};
Run Code Online (Sandbox Code Playgroud)
然后就可以成功实例化了。情况总是如此吗?为什么?为每个包含具有用户定义的默认构造函数的成员的联合编写一个空的构造函数和析构函数似乎很愚蠢。