Skip to main content
return to the original description, and include -O0 in the language spec
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55

C++ (GCC -O0)

#include <cstdlib>
#include <cstdint>
#include <iostream>

void *aCaller = NULL;
bool a()
{
    aCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return true;
}
bool b()
{
    void *bCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return abs(((char*)bCaller - (char*)aCaller)) > 9;
}

int main()
{
    if (a()) std::cout << "a is true" << std::endl;
    if (b()) std::cout << "b is true" << std::endl;
    if (a() && b()) std::cout << "a && b is true" << std::endl;
    return 0;
}

Attempt This Online!

As of submission, this is the only solution so far that doesn't redefineuse a different/redefined AND or use operator precedence tricks, but rather uses a straight-up logical AND.

The trick is that a() and b() are functions, the latter of which returns false if it's been called very close after a() was last called. As such it's probably not very portable, and if it is, at the very least the threshold of 9 bytes may need to be modified to work on different architectures.

C++ (GCC)

#include <cstdlib>
#include <cstdint>
#include <iostream>

void *aCaller = NULL;
bool a()
{
    aCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return true;
}
bool b()
{
    void *bCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return abs(((char*)bCaller - (char*)aCaller)) > 9;
}

int main()
{
    if (a()) std::cout << "a is true" << std::endl;
    if (b()) std::cout << "b is true" << std::endl;
    if (a() && b()) std::cout << "a && b is true" << std::endl;
    return 0;
}

Attempt This Online!

As of submission, this is the only solution so far that doesn't redefine AND or use operator precedence tricks, but rather uses a straight-up logical AND.

The trick is that a() and b() are functions, the latter of which returns false if it's been called very close after a() was last called. As such it's probably not very portable, and if it is, at the very least the threshold of 9 bytes may need to be modified to work on different architectures.

C++ (GCC -O0)

#include <cstdlib>
#include <cstdint>
#include <iostream>

void *aCaller = NULL;
bool a()
{
    aCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return true;
}
bool b()
{
    void *bCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return abs(((char*)bCaller - (char*)aCaller)) > 9;
}

int main()
{
    if (a()) std::cout << "a is true" << std::endl;
    if (b()) std::cout << "b is true" << std::endl;
    if (a() && b()) std::cout << "a && b is true" << std::endl;
    return 0;
}

Attempt This Online!

As of submission, this is the only solution so far that doesn't use a different/redefined AND or operator precedence tricks, but rather uses a straight-up logical AND.

The trick is that a() and b() are functions, the latter of which returns false if it's been called very close after a() was last called. As such it's probably not very portable, and if it is, at the very least the threshold of 9 bytes may need to be modified to work on different architectures.

Rollback to Revision 1
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55

C++ (GCC)

#include <cstdlib>
#include <cstdint>
#include <iostream>

void *aCaller = NULL;
bool a()
{
    aCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return true;
}
bool b()
{
    void *bCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return abs(((char*)bCaller - (char*)aCaller)) > 9;
}

int main()
{
    if (a()) std::cout << "a is true" << std::endl;
    if (b()) std::cout << "b is true" << std::endl;
    if (a() && b()) std::cout << "a && b is true" << std::endl;
    return 0;
}

Attempt This Online!

ThisAs of submission, this is the only solution so far that doesn't redefine AND or use operator precedence tricks, but rather uses a straight-up uses logical AND.

The trick is that a() and b() are functions, the latter of which returns false if it's been called very close after a() was last called. As such it's probably not very portable, and if it is, at the very least the threshold of 9 bytes may need to be modified to work on different architectures.

C++ (GCC)

#include <cstdlib>
#include <cstdint>
#include <iostream>

void *aCaller = NULL;
bool a()
{
    aCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return true;
}
bool b()
{
    void *bCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return abs(((char*)bCaller - (char*)aCaller)) > 9;
}

int main()
{
    if (a()) std::cout << "a is true" << std::endl;
    if (b()) std::cout << "b is true" << std::endl;
    if (a() && b()) std::cout << "a && b is true" << std::endl;
    return 0;
}

Attempt This Online!

This straight-up uses logical AND.

The trick is that a() and b() are functions, the latter of which returns false if it's been called very close after a() was last called. As such it's probably not very portable, and if it is, at the very least the threshold of 9 bytes may need to be modified to work on different architectures.

C++ (GCC)

#include <cstdlib>
#include <cstdint>
#include <iostream>

void *aCaller = NULL;
bool a()
{
    aCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return true;
}
bool b()
{
    void *bCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return abs(((char*)bCaller - (char*)aCaller)) > 9;
}

int main()
{
    if (a()) std::cout << "a is true" << std::endl;
    if (b()) std::cout << "b is true" << std::endl;
    if (a() && b()) std::cout << "a && b is true" << std::endl;
    return 0;
}

Attempt This Online!

As of submission, this is the only solution so far that doesn't redefine AND or use operator precedence tricks, but rather uses a straight-up logical AND.

The trick is that a() and b() are functions, the latter of which returns false if it's been called very close after a() was last called. As such it's probably not very portable, and if it is, at the very least the threshold of 9 bytes may need to be modified to work on different architectures.

I no longer want to frame the description as a comparison against previous submissions.
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55

C++ (GCC)

#include <cstdlib>
#include <cstdint>
#include <iostream>

void *aCaller = NULL;
bool a()
{
    aCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return true;
}
bool b()
{
    void *bCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return abs(((char*)bCaller - (char*)aCaller)) > 9;
}

int main()
{
    if (a()) std::cout << "a is true" << std::endl;
    if (b()) std::cout << "b is true" << std::endl;
    if (a() && b()) std::cout << "a && b is true" << std::endl;
    return 0;
}

Attempt This Online!

As of submission, this is the only solution so far that doesn't redefine AND or use operator precedence tricks, but rather uses aThis straight-up uses logical AND.

The trick is that a() and b() are functions, the latter of which returns false if it's been called very close after a() was last called. As such it's probably not very portable, and if it is, at the very least the threshold of 9 bytes may need to be modified to work on different architectures.

C++ (GCC)

#include <cstdlib>
#include <cstdint>
#include <iostream>

void *aCaller = NULL;
bool a()
{
    aCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return true;
}
bool b()
{
    void *bCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return abs(((char*)bCaller - (char*)aCaller)) > 9;
}

int main()
{
    if (a()) std::cout << "a is true" << std::endl;
    if (b()) std::cout << "b is true" << std::endl;
    if (a() && b()) std::cout << "a && b is true" << std::endl;
    return 0;
}

Attempt This Online!

As of submission, this is the only solution so far that doesn't redefine AND or use operator precedence tricks, but rather uses a straight-up logical AND.

The trick is that a() and b() are functions, the latter of which returns false if it's been called very close after a() was last called. As such it's probably not very portable, and if it is, at the very least the threshold of 9 bytes may need to be modified to work on different architectures.

C++ (GCC)

#include <cstdlib>
#include <cstdint>
#include <iostream>

void *aCaller = NULL;
bool a()
{
    aCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return true;
}
bool b()
{
    void *bCaller = __builtin_extract_return_addr(__builtin_return_address(0));
    return abs(((char*)bCaller - (char*)aCaller)) > 9;
}

int main()
{
    if (a()) std::cout << "a is true" << std::endl;
    if (b()) std::cout << "b is true" << std::endl;
    if (a() && b()) std::cout << "a && b is true" << std::endl;
    return 0;
}

Attempt This Online!

This straight-up uses logical AND.

The trick is that a() and b() are functions, the latter of which returns false if it's been called very close after a() was last called. As such it's probably not very portable, and if it is, at the very least the threshold of 9 bytes may need to be modified to work on different architectures.

Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading