decltype question

I've copied this snippet of code from a new C++ book, "Learning C++ Functional Programming" by Anggorro.

It doesn't compile though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <typeinfo>

const int func1();
const int& func2();

struct X { double d; };

auto main() -> int {
  int i{0};

  const X* x = new X();

  decltype(func1()) f1;
  decltype(func2()) f2;
  decltype(i) i1;

  std::cout << "Declare type func1() " << f1<< "\n";
  std::cout << "Declare type func2() " << f2<< "\n";
  std::cout << "Declare type i " << i1 << std::endl;

  return 0;
}

const int func1() { return 1; }

const int & func2() { return 2; }


The following compile error occurs, using g++ on Linux:
 
15:21: error: ‘f2’ declared as reference but not initialized


Code sample is from pg 11 of the book.
Can anyone explain why the error?
You can't return a reference to a number, because that would allow you to do:
 
getRefToNumber() = 6;

Which could result in writing 2 = 6, thus assigning the value of 6 to 2. This isn't possible because 2 is an rvalue and not lvalue.

Your question has nothing to do with decltype.
Second part of the answer: Since your func2() returns a reference, you have to initialize f2. All references should be initialized at creation.
func2() should return const ref to const int as mentioned above, also the program gives garbage values because f1, f2, il were not initialized:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
//#include <typeinfo>

const int func1();
const int& func2();

struct X { double d; };

auto main() -> int {
  int i{0};

  const X* x = new X(); //unused variable

  decltype(func1()) f1{};
  decltype(func2()) f2{};
  decltype(i) i1{};

  std::cout << "Declare type func1() " << f1<< "\n";
  std::cout << "Declare type func2() " << f2<< "\n";
  std::cout << "Declare type i " << i1 << std::endl;

  return 0;
}

const int func1() { return 1; }

const int & func2() { const int i {2}; return i; }

for func1() there's the additional warning:
 
warning: type qualifiers ignored on function return type [-Wignored-qualifiers] 

https://stackoverflow.com/questions/1134237/pedantic-gcc-warning-type-qualifiers-on-function-return-type
const int & func2() { const int i {2}; return i; }
This engenders undefined behaviour.
yeah, you're right i think
Hi

thanks for the replies, so as long as the variables to functions that return a reference are initialised it compiles, something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <typeinfo>

const int func1();
const int& func2();

struct X { double d; };

auto main() -> int {
  int i{0};

  const X* x = new X();

  decltype(func1()) f1;
  decltype(func2()) f2 = f1;
  decltype(i) i1;
  decltype(x->d) d1;
  decltype((x->d)) d2 = d1;

  std::cout << "Declare type func1() " << f1<< "\n";
  std::cout << "Declare type func2() " << f2<< "\n";
  std::cout << "Declare type i " << i1 << std::endl;
  std::cout << "Declare type d1 " << d1 << std::endl;
  std::cout << "Declare type d2 " << d2 << std::endl;

  return 0;
}

const int func1() {
  return 1;
}

const int & func2() {
  int temp{2};
  int *  n = new int;
  *n = temp;
  return *n;
}


I understand f1 & f2, but I don't understand the following 2 declarations:
1
2
  decltype(x->d) d1;
  decltype((x->d)) d2 = d1;


which occur on page 12 of the book. Firstly , what is "d", and secondly
why do I have to initialise " decltype((x->d)) d2" to d1 in order for the code to compile ?



Last edited on
why do I have to initialise decltype((x->d)) d2

Because the type decltype((x->d)) is a lvalue reference (because the expression x->d is an lvalue, and it is parenthesized.)
http://en.cppreference.com/w/cpp/language/decltype
Last edited on
Topic archived. No new replies allowed.