我花了一个小时来研究一个大类,我需要将 的成员函数传递给一个最小的示例。给出的编译器错误是:
错误 C2664: 'B::B(const B &)': 无法将参数 1 从 '_Ty' 转换为 'const B &'
下面是一个最小的示例,当使用指向互斥体的指针时,代码可以正常编译。有人可以帮助我理解这里出了什么问题以及为什么以及如何从“元组”中的错误消息中得到提示吗?
感谢大家!
#include <functional>
#include <iostream>
#include <string>
#include <mutex>
class A {
public:
std::string Init(std::function<std::string(void)> f) {
return f();
}
};
class B {
std::mutex lock;
std::string member = "Hello World!";
public:
std::string foo() {
return member;
}
};
int main() {
auto callback3 = std::bind(&B::foo, B());
auto instance = A();
std::cout << instance.Init(callback3) << "\n";
}
Run Code Online (Sandbox Code Playgroud) A. Williams在他的《C++ Concurrency in Action》一书中引入了锁层次结构的概念作为死锁避免机制。下面,我报告了一个实现的精简版本HierarchicalMutex(摘自书中):
class HierarchicalMutex {
private:
std::mutex Mutex_;
unsigned const Level_;
unsigned PrevLevel_{0};
static thread_local unsigned current_level;
public:
explicit HierarchicalMutex(unsigned level) : Level_{level} {}
void lock() {
if (current_level <= this->Level_) { // (1)
// I can only lock a mutex with a lower level than the currently
// locked one.
throw std::logic_error("lock: Out of order");
}
this->Mutex_.lock();
this->PrevLevel_ = std::exchange(current_level, this->Level_);
}
// try_lock implemented accordingly [...]
void unlock() {
if (current_level …Run Code Online (Sandbox Code Playgroud) 让我们考虑以下用于阶乘函数的编译时计算的代码:
#include <concepts>
template <std::integral auto num>
struct factorial {
constexpr static auto val{num * factorial<num - 1>::val};
};
// Note: This only specializes the class for (int)0
template <>
struct factorial<0> {
constexpr static auto val{1};
};
// ...
factorial<4>::val; // Ok: 24
factorial<4u>::val; // error: template instantiation depth exceeds maximum of 900
// This makes sense: There's no factorial<0u> specialization.
Run Code Online (Sandbox Code Playgroud)
factorial<0>有没有办法为所有整型(即满足 的所有类型)引入专门化std::integral?
当然,我想避免实际写出专业化factorial<0u>,factorial<0l>等等。
我刚刚遇到std::as_const,并对以下代码片段中最后一行的输出感到惊讶:
#include <cstdio>
#include <utility>
struct S {
void foo() { std::puts("foo: non const"); }
void foo() const { std::puts("foo: const"); }
};
int main() {
S s;
s.foo(); // foo: non const
std::as_const(s).foo(); // foo: const
auto* s_ptr = &s;
s_ptr->foo(); // foo: non const
std::as_const(s_ptr)->foo(); // foo: non const (?)
}
Run Code Online (Sandbox Code Playgroud)
查看文档,我明白为什么调用
非const重载:返回 a ,即对非常量的常量指针的引用,而不是如我所料的a ,即指向常量的指针。foostd::as_const(s_ptr)S* const&SS const*S
所以,我的问题是为什么标准不提供std::as_const指针类型的重载?例如:
template <class T>
constexpr std::add_const_t<T>* as_const(T* t) …Run Code Online (Sandbox Code Playgroud) 我对 C++ 非常陌生,并且在创建课程时遇到问题。
所以我有这个 pad 类,它具有doublepad 侧面的 x 和 y 坐标的输入。详细来说,假设你有一个 2cmx2cm 的垫子,它会有pad({0.0,2.0},{0.0,2.0})。我想将默认构造函数设置为 0x0 的 pad。
class pad {
public:
double xcor[2] = {0, 0};
double ycor[2] = {0, 0};
double charge = 0;
pad() = default; // put this for now to work on code that works with the
// pad object
pad(double xcord[], double ycord[]) {
for (int i = 0; i < 2; i++) {
xcor[i] = xcord[i];
ycor[i] = ycord[i];
}
} …Run Code Online (Sandbox Code Playgroud) 我对函数模板的理解一直是:如果它们包含无效的 C++ 并且您没有实例化它们,您的项目将正常编译。
然而,下面的代码:
#include <cstdio>
#include <utility>
template <typename T>
void contains_compile_time_error(T&& t) {
int j = nullptr;
}
int main() {}
Run Code Online (Sandbox Code Playgroud)
编译为:
x86-64 gcc 11.2和旗帜-std=c++20;x64 msvc v19.31和旗帜/std:c++20;并且不与x86-64 clang 14.0.0(带有标志-std=c++{11,14,17,20}):
error: cannot initialize a variable of type 'int' with an rvalue of type 'std::nullptr_t'
int j = nullptr;
^ ~~~~~~~
1 error generated.
Run Code Online (Sandbox Code Playgroud)
更让我困惑的是下面的代码:
error: cannot initialize a variable of type 'int' with an rvalue of type …Run Code Online (Sandbox Code Playgroud)