我希望下面的代码无法编译,因为asf
的成员函数没有提供所需的成员。实际上,即使关闭优化,它在不使用时也可以很好地编译。这怎么可能?Exclamator<B>
B
name
f
#include <string>
#include <iostream>
class A {
public:
std::string name() { return "A"; }
};
class B {} ;
template <typename T>
class Exclamator {
public:
Exclamator(T a): x{a} {}
void f() {
std::cout << x.name() << std::endl;
}
private:
T x;
};
int main() {
A a;
Exclamator xa {a};
xa.f();
B b;
Exclamator xb {b};
// xb.f();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
$ g++ -std=c++17 -O0 main.cpp
$ ./a.out
A
Run Code Online (Sandbox Code Playgroud) 如果你有这样的事情:
#!/bin/bash
(
x=foo
{ echo $x }
)
Run Code Online (Sandbox Code Playgroud)
括号和大括号的意义是什么?这些结构称为什么?他们有什么属性?它们用于什么等?