#include <type_traits>\n\nint main()\n{\n auto f = [] {};\n static_assert(std::is_same_v<decltype(!f), bool>); // ok\n\n f.operator bool(); // error: \xe2\x80\x98struct main()::<lambda()>\xe2\x80\x99 \n // has no member named \xe2\x80\x98operator bool\xe2\x80\x99\n}\nRun Code Online (Sandbox Code Playgroud)\nC++ 是否保证 lambda 未命名类始终具有已operator bool()定义的?
#include <set>\n#include <iostream>\nusing namespace std;\n\ntemplate<class T> \nclass A \n{ \n public:\n A(T a = 0,T b =0): m_a(a),m_b(b) {}\n\n bool operator<(const A& lhs)\n {\n /* compare code */\n }\n \n private:\n T m_a;\n T m_b; \n}; \n\nint main() \n{\n A<int> abc(2,3);\n\n set<A<int>> P2D;\n P2D.insert(abc);\n return 0; \n}\nRun Code Online (Sandbox Code Playgroud)\n当我运行此代码时,出现以下错误
\n\n\n操作数类型为 \xe2\x80\x98const A\xe2\x80\x99 和 \xe2\x80\x98const A\xe2\x80\x99
\n
const如果我在重载上声明关键字operator<:
bool operator<(const A& lhs) const\n{\n /* compare code */\n}\nRun Code Online (Sandbox Code Playgroud)\n它没有给出错误,但是:
\n我希望使用运算符 [] 中传递的表达式。我认为使用变量模板参数可以解决问题,但我错了......在 c++11 中是否有一种方法可以做到这一点?
class object {
private:
public:
void values() { std::cout << "finished" << std::endl; }
template <typename T, typename... Type> void values(T arg, Type... args) {
std::cout << arg << " " << std::endl;
values(args...);
}
template<typename... Type> void operator[](Type... args) {
values(args...);
}
};
int main(void) {
object o1 = object();
o1.values(1, 6.2, true, "hello"); // Works fine.
o1[1, 6.2, true]; // Only the last value gets printed eg. true
return 0;
}
Run Code Online (Sandbox Code Playgroud)
更广泛的目标是我被要求为此制定一个工作语法
let …Run Code Online (Sandbox Code Playgroud) 这是代码:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <utility>
struct student
{
std::string full_name;
int group;
int number;
friend std::istream& operator>>(std::istream& in, student& obj)
{
in >> obj.full_name >> obj.group >> obj.number;
return in;
}
friend std::ostream& operator<<(std::ostream& out, const student& obj)
{
out << "Full name: " << obj.full_name << '\n';
out << "Group: " << obj.group << '\n';
out << "Number: " << obj.number << '\n';
return out;
}
bool operator<(const student& obj)
{
return this->number …Run Code Online (Sandbox Code Playgroud) class Train{
public:
char direction;
int loading_time, crossing_time;
...
friend std::ostream& operator<<(std::ostream& os, const Train& t){
os << t.direction << '/' << t.loading_time << '/' << t.crossing_time;
return os;
}
};
Run Code Online (Sandbox Code Playgroud)
为什么在这种情况下需要“朋友”?所有属性都是公共的。我应该只使用结构体吗?
Python 3 允许重载*.
例如,
a*b
允许a通过提供方法来定义结果__mul__。
然而,如果该方法返回,则调用NotImplemented,该__rmul__方法b(如果存在)以给出结果。此处记录了这一点。
这样做的一个优点是我可以在b不a知道a.
C++ 中是否有等效的方法__rmul__,或者是否有实现相同或类似事物的方法?
假设我有一个非常简单的 Rational 类,如下所示:
class Rational
{
int numer;
int denom;
public:
Rational(const int& numer, const int& denom) : numer(numer), denom(denom) {}
void operator*(const Rational& other) { std::cout << "multiply, simple as\n"; }
}
Run Code Online (Sandbox Code Playgroud)
一切都很好。然后说我希望能够将 Rational 类与整数相乘,因此我向该类添加另一个函数:
class Rational
{
int numer;
int denom;
public:
Rational(const int& numer, const int& denom) : numer(numer), denom(denom) {}
void operator*(const Rational& other) { std::cout << "multiply, simple as\n"; }
void operator*(const int& other) { std::cout << "some math here\n"; }
}
Run Code Online (Sandbox Code Playgroud)
好吧,没什么大不了的。除了我实际上无法执行以下操作,因为参数的顺序都是错误的:
Rational mine(1, …Run Code Online (Sandbox Code Playgroud) 我创建了一个类并重载了new和delete运算符来打印分配/释放的内存大小。在下面的示例中,它分配了 28 个字节,但释放了 4 个字节。为什么?
#include <iostream>
#include <string>
using namespace std;
class Person
{
private:
string name;
int age;
public:
Person() {
cout << "Constructor is called.\n";
}
Person(string name, int age) {
this->name = name;
this->age = age;
}
void* operator new(size_t size) {
void* p = malloc(size);
cout << "Allocate " << size << " bytes.\n";
return p;
}
void operator delete(void* p) {
free(p);
cout << "Free " << sizeof(p) << " …Run Code Online (Sandbox Code Playgroud) 我有一个名为“Vector”的类。它由两个私有字段组成:std::vector<double> coordinates和int len。方法dim()返回len。
我<<像这样重载运算符:
friend std::ostream& operator<<(std::ostream& os, Vector& vec )
{
std:: cout << "(";
for ( int i = 0; i < vec.dim(); i++ ) {
if ( i != vec.dim()-1){
os << vec[i] << ", ";
} else {
os << vec[i];
}
}
os << ')';
return os;
}
Run Code Online (Sandbox Code Playgroud)
+像这样的运算符:
friend Vector operator +(Vector& first, Vector& second)
{
if(first.dim() != second.dim()){
throw std::length_error{"Vectors must be …Run Code Online (Sandbox Code Playgroud) 我有这么一小段代码:
template<typename T>
class Test
{
public:
//operator T() const { return toto; }
T toto{ nullptr };
};
void function(int* a) {}
int main(int argc, char** argv)
{
Test<int*> a;
function(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
除非该行未被注释,否则它不会编译operator T() const { return toto; }。这神奇地起作用,但我不确定为什么(如果我取消注释该行)。
a我确实理解,如果该行被注释,则传递给的类型function()与预期的类型不兼容int*。所以,编译器当然会抱怨……没问题。
我还了解运算符返回对象的实际类型,因此在这种特殊情况下编译器很高兴。
我不明白为什么在这种特殊情况下调用操作员。
做与function(a)做同样的事情function(a()),只是()隐式的吗?