-1

I have this exercise in which we try various combinations of rvalue and lvalue references using a template class, I am getting two assertion errors; if someone could guide.

#include <assert.h>

typedef int& IntLRef;
typedef IntLRef& IntLLRef;
typedef IntLRef&& IntLRRef;

typedef int&& IntRRef;
typedef IntRRef& IntRLRef;
typedef IntRRef&& IntRRRef;

template<typename T, typename U>
struct IsSameType
{
  static const bool value = false;
};

template<typename T>
struct IsSameType <T, T>
{
    static const bool value = true;
};
static_assert(IsSameType<IntLRef, IntLLRef>::value, "LRef DIF LLRef"); static_assert(IsSameType<IntLRef, IntLRRef>::value, "LRef DIF LRRef"); static_assert(IsSameType<IntLLRef, IntLRRef>::value, "LLRef DIF LRRef");

static_assert(IsSameType<IntRRef, IntRLRef>::value, "RRef DIF RLRef"); static_assert(IsSameType<IntRRef, IntRRRef>::value, "RRef DIF RRRef"); static_assert(IsSameType<IntRLRef, IntRRRef>::value, "RLRef DIF RRRef");

int main();

I am getting assertion error :

rvalue_ex3.cpp:34:48: error: static assertion failed: RRef DIF RLRef
   34 |   static_assert(IsSameType<IntRRef, IntRLRef>::value, "RRef DIF RLRef");
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
rvalue_ex3.cpp:36:49: error: static assertion failed: RLRef DIF RRRef
   36 |   static_assert(IsSameType<IntRLRef, IntRRRef>::value, "RLRef DIF RRRef");
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
akm009@a

I need to make modfications to assert it as true and understand why it is failing

Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39
AKM
  • 34
  • 6
  • its not clear what you are asking or what you want to achieve. You can assert `! IsSameType<...` , but I suppose thats not what you want, what do you want? – 463035818_is_not_an_ai Nov 28 '22 at 10:33
  • 2
    Btw: Your `IsSameType` functionality is already available via the standard library, including a variable template: https://en.cppreference.com/w/cpp/types/is_same E.g. `IsSameType::value` could be replaced with `std::is_same_v` Also you should include `cassert` instead of `assert.h` when using C++ and you do not need this include for `static_assert` to be available in C++... – fabian Nov 28 '22 at 10:45
  • Note: the special properties of the `main` function make a declaration of the `main` function pointless in 99.99% of all cases. It must not be called anywhere in your program, so knowing the exact signature is usually of no benefit to you... – fabian Nov 28 '22 at 10:53

1 Answers1

5

This is called reference collapsing:

It is permitted to form references to references through type manipulations in templates or typedefs, in which case the reference collapsing rules apply: rvalue reference to rvalue reference collapses to rvalue reference, all other combinations form lvalue reference

(emphasis added)

Which means that IntRRef which is int&& is not the same as IntRLRef because the latter is defined as IntRRef& which is an lvalue reference to an rvalue reference and thus collapses to an lvalue reference: int&.

bitmask
  • 32,434
  • 14
  • 99
  • 159