这是一个模板函数,它接受一个指针(或类似指针的对象)和一个成员函数:
template <typename Ptr, typename MemberFunctor>
int example(Ptr ptr, MemberFunctor func )
{
return (ptr->*func)();
}
Run Code Online (Sandbox Code Playgroud)
如果与普通指针一起使用时有效:
struct C
{
int getId() const { return 1; }
};
C* c = new C;
example(c, &C::getId); // Works fine
Run Code Online (Sandbox Code Playgroud)
但它不适用于智能指针:
std::shared_ptr<C> c2(new C);
example(c2, &C::getId);
Run Code Online (Sandbox Code Playgroud)
错误信息:
error: C2296: '->*' : illegal, left operand has type 'std::shared_ptr<C>'
Run Code Online (Sandbox Code Playgroud)
为什么?以及如何制作适合两者的东西?
我有JFrame的jButton1私有成员,我想在单击按钮时关闭框架.
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
Run Code Online (Sandbox Code Playgroud)
我想做,super.close()但找不到超级超级.有没有办法引用JFrame
为什么赋值运算符在声明对象的同一行中完成时不允许使用lambda表达式?
它似乎在MSVC中工作.
测试代码:https: //godbolt.org/g/n2Tih1
class Func
{
typedef void(*func_type)();
func_type m_f;
public:
Func() {}
Func(func_type f) : m_f(f) {}
Func operator=(func_type f) {
m_f = f;
return *this;
}
};
int main()
{
// doesn't compile in GCC and clang, it does in MSVC
Func f1 = []() {
};
// compiles!
Func f2;
f2 = []() {
};
// compiles!
Func f3([]() {
});
}
Run Code Online (Sandbox Code Playgroud) 我正在学习C++,我正在尝试实现一个二进制搜索函数,它找到谓词所在的第一个元素.函数的第一个参数是向量,第二个参数是一个计算给定元素的谓词的函数.二进制搜索功能如下所示:
template <typename T> int binsearch(const std::vector<T> &ts, bool (*predicate)(T)) {
...
}
Run Code Online (Sandbox Code Playgroud)
如果像这样使用,这可以按预期工作:
bool gte(int x) {
return x >= 5;
}
int main(int argc, char** argv) {
std::vector<int> a = {1, 2, 3};
binsearch(a, gte);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用lambda函数作为谓词,我会收到编译器错误:
search-for-a-range.cpp:20:5: error: no matching function for call to 'binsearch'
binsearch(a, [](int e) -> bool { return e >= 5; });
^~~~~~~~~
search-for-a-range.cpp:6:27: note: candidate template ignored: could not match 'bool (*)(T)' against '(lambda at
search-for-a-range.cpp:20:18)'
template <typename T> int …Run Code Online (Sandbox Code Playgroud) 我有以下示例代码:
class A {
public:
static int a;
};
int A::a = 0;
class B {
public:
static A a1;
};
A B::a1;
class C {
public:
static A a1;
};
A C::a1;
int main(int argc, const char * argv[]) {
C::a1.a++;
B::a1.a++;
std::cout << B::a1.a << " " << C::a1.a << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
B类和C类将A类作为静态成员变量.
我希望程序打印"1 1",但它打印"2 2".
如果多个类有一个共同的静态变量,它们是否共享(在同一范围内?)
请注意,我std::thread只是用来获取错误中的可读类型:
int main() {
const int * first;
using deref = decltype(*first);
std::thread s = std::remove_const<deref>::type{}; // const int ???
std::thread s2 = deref{}; // const int
std::thread s3 = std::remove_const<const int>::type{}; // int
}
Run Code Online (Sandbox Code Playgroud)
似乎remove_const<deref>::type是const int,不像int我期望的那样可变.
我知道在 C++ 中,类头中成员的声明定义了初始化顺序。你能告诉我为什么 C++ 选择这种设计吗?强制初始化顺序而不是遵循初始化列表有什么好处吗?
以下C++ 11程序是否格式错误?
const int x[] = {1,2,3};
static_assert(x[0] == 1, "yay");
int main() {}
Run Code Online (Sandbox Code Playgroud)
gcc和clang似乎这么认为,但为什么不是x[0] == 1一个恒定的表达?
x[0] == 1
subscript operator
*(x+0) == 1
array-to-pointer conversion (int* p = x)
*(p+0) == 1
pointer addition
*p == 1
indirection (lvalue y = x[0])
y == 1
lvalue-to-rvalue conversion:
Run Code Online (Sandbox Code Playgroud)
一个非易失性glvalue(是的,x [0]是glvalue和非易失性)的整数(是的,它有类型const int)或枚举类型引用一个非易失性的const对象(是的它有类型const int)使用前面的初始化(是初始化为1),使用常量表达式初始化(是1是常量表达式)
似乎是真的,x数组的第一个元素满足这些条件.
1 == 1
Run Code Online (Sandbox Code Playgroud)
?
这是编译器错误,标准缺陷,还是我错过了什么?
5.19 [expr.const]的哪一部分说这不是一个常量表达式?
什么是声明的返回值/类型int i = 5?
为什么不编译这段代码:
#include <iostream>
void foo(void) {
std::cout << "Hello";
}
int main()
{
int i = 0;
for(foo(); (int i = 5)==5 ; ++i)std::cout << i;
}
Run Code Online (Sandbox Code Playgroud)
虽然这样做
#include <iostream>
void foo(void) {
std::cout << "Hello";
}
int main()
{
int i = 0;
for(foo(); int i = 5; ++i)std::cout << i;
}
Run Code Online (Sandbox Code Playgroud) 在下面的代码中
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a,c="!";
cin>>a;
int l=a.size();
for(int i=0;i<l;i++)
{
c=c+"#"+a[i];
}
cout<<c;
}
Run Code Online (Sandbox Code Playgroud)
如果我替换c=c+"#"+a[i]为c+="#"+a[i]我会得到意外的输出。第二种情况下的输出是 !boxboxboxbox 与https://www.onlinegdb.com/上的输入无关。在“dev c++”上,输出是 -
但是 a += b 等价于 a = a + b 。那么造成输出差异的原因是什么呢?