2

Why I cant define this function,

int *clone() const &
{ 
    return new int(10); 
}

or

int x;
int *clone() const &&
{ 
    return new int(std::move(x)) ; 
}

I should be able to add const qualifier functions. Should I include any headers?

olevegard
  • 4,780
  • 1
  • 22
  • 28
Tamim Addari
  • 6,271
  • 9
  • 37
  • 52
  • 4
    What's the `&` and `&&` supposed to do there? – Luchian Grigore Jun 19 '13 at 07:46
  • What Luchian Grigore said. Also, if x is a member variable and clone() a const method, std::move(x) would violate const-correctness if allowed to compile (because it would allow modifying x). – Medinoc Jun 19 '13 at 07:48
  • and instead of returning a pointer to an int, its probably better to return the int itself – Enigma Jun 19 '13 at 07:49
  • Its not the actual function, the actual is a big class,rather than int, I just simplified because problem is in qualifiers. – Tamim Addari Jun 19 '13 at 08:02
  • @LuchianGrigore: well you should open the C++ standard then. cf my comment to doctorlove's reply. – v.oddou Dec 09 '13 at 04:08
  • @v.oddou any C++ question can be answered with "open the C++ standard", so why bother posting on SO if you can't ask questions? – Luchian Grigore Dec 09 '13 at 07:41

3 Answers3

3

I'm new to c++ and I am in the same error with you. I compiled the sample code, from C++ Primer 5th Edition, which described the reference qualifier. However, my GNU compiler showed me error. I suppose that current compilers do not support this new feature introduced in C++11. And it seems not many people know this because very few information can be found on the Internet. Maybe later compilers will support this feature.

Many c++11 features haven't been supported... I've met some before.

This is part of the sample code, similar to yours:

Foo sorted() &&;
Foo sorted() const &
Tim Zhang
  • 73
  • 1
  • 7
0

Because any qualifier after function name applies to this pointer. if you want to make constant this pointer
you should just do overloading by : int *clone() const

Anand Rathi
  • 746
  • 4
  • 11
0

You can use the r-value reference on parameters, for example in a move assignment or move constructor. It seems clang has been trying an extension called 'called "rvalue reference for *this"', but I suggest you work through the move constructors and assignment operators first.

Community
  • 1
  • 1
doctorlove
  • 17,477
  • 2
  • 41
  • 57
  • C++11 clearly specifies the ref-qualifier on functions. see `8.3.5 [dcl.fct]`. [gcc support](http://gcc.gnu.org/ml/gcc-patches/2013-04/msg00024.html). You all need to give OP a break. – v.oddou Dec 09 '13 at 04:07