我试图在编译时确定所有传递对象的大小,然后在超过最大大小时通过 static_assert 中止构建过程。
#include <iostream>
template<class T>
class Test
{
public:
T value;
constexpr size_t size() const { return sizeof(T) + 3; }
};
template<typename ...T>
constexpr int calc(const T&...args)
{
return (args.size() + ...);
}
template<typename ...T>
void wrapper(const T& ...args)
{
// error: 'args#0' is not a constant expression
constexpr int v = calc(args...);
static_assert(v <= 11, "oops");
}
int main()
{
Test<int> a;
Test<char> b;
// a.size() + b.size() == 11
// works
constexpr int v = …Run Code Online (Sandbox Code Playgroud) 有人可以解释一下为什么这段代码无法构建:
std::function<void(int)> intcb = [](int a)
{
std::cout << "callback: " << a << std::endl;
};
std::list<std::function<void(int)>> list;
list.push_back(intcb);
list.remove(intcb);
Run Code Online (Sandbox Code Playgroud)
构建错误位于删除函数中:
no match for 'operator==' (operand types are 'std::function<void(int)>' and 'const value_type' {aka 'const std::function<void(int)>'})
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助