This question already has an answer here:
Reading this blog bost I understood that the following won't compile:
#include <iostream>
using namespace std;
int* foo() {
return new int{1};
}
int main()
{
if (int* a = foo() && *a == 1)
cout << "yes";
else
cout << "no";
}
GCC (http://tpcg.io/UMl20e) gives:
main.cpp: In function ‘int main()’: main.cpp:11:33: error: cannot convert ‘bool’ to ‘int*’ in initialization if (int* a = foo() && *a == 1)
As I have such checks often: Is there an equivalently "beautiful" oneliner for the if condition which does compile?
One possible solution would be
if (int* a = foo()) if(*a == 1)
but this looks somewhat suspicious
Aucun commentaire:
Enregistrer un commentaire