const double & rx

The non-const lvalue reference matches the lvalue argument w. The other two arguments are rvalues, and the const lvalue reference can refer to copies of them.

b.

double & rx

double && rx

double && rx

The lvalue reference matches the lvalue argument w, and the rvalue references matches the two rvalue arguments.

c.

const double & rx

double && rx

double && rx

The const lvalue reference matches the lvalue argument w, and the rvalue reference matches the two rvalues.

In short, a non-const lvalue parameter matches an lvalue argument, a non-const rvalue parameter matches an rvalue argument, and a const lvalue parameter can match either an lvalue or an rvalue argument, but the compiler will prefer the first two choices, if available.

4. They are the default constructor, the copy constructor, the move constructor, the destructor, the copy assignment operator, and the move assignment operator. They are special because the compiler can automatically provide defaulted versions of these functions, depending on the context.

5. A move constructor can be used when it makes sense to transfer ownership of data instead of copying it, but there is no mechanism for transferring ownership of a standard array. If the Fizzle class used a pointer and dynamic memory allocation, then one can transfer ownership by reassigning the address of the data to a new pointer.

6.

#include

#include

template

  void show2(double x, T fp) {std::cout << x << " -> " << fp(x) << '\n';}

int main()

{

    show2(18.0, [](double x){return 1.8*x + 32;});

    return 0;

}

7.

#include

#include

#include

const int Size = 5;

template

  void sum(std::array a, T& fp);

int main()

{

    double total = 0.0;

    std::array temp_c = {32.1, 34.3, 37.8, 35.2, 34.7};

    sum(temp_c, [&total](double w){total += w;});

    std::cout << "total: " << total << '\n';

    std::cin.get();

    return 0;

}

template

  void sum(std::array a, T& fp)

{

    for(auto pt = a.begin(); pt != a.end(); ++pt)

    {

        fp(*pt);

    }

}

Index

Symbols

<=, 217

+ (addition operator), overloading, 569–572

-= (assignment operator), 212

*= (assignment operator), 212

+= (assignment operator), 211

%= (assignment operator), 212

/= (assignment operator), 212

= (assignment operator), 43–44, 644, 767–768, 772–775

compared to equality operator, 218–220

custom definitions, 645–646

enumerator values, setting, 152

overloading, 652–658

sayings1.cpp, 656

string1.cpp, 653–656

string1.h, 652–653

potential problems, 645

strings, 133–134

structures, 145–146

when to use, 644

& (bitwise AND operator), 1239–1240

~ (bitwise negation operator), 1237

| (bitwise OR operator), 1237–1238

^ (bitwise XOR operator), 1238

{} (braces), 258

[] (brackets), 649–651

, (comma operator), 214–217

example, 214–216

precedence, 217

/*...*/ comment notation, 33

// comment notation, 27, 32

+ (concatenation operator), strings, 133–134

?: (conditional operator), 273–274

- - (decrement operator), 207–208

pointers, 210–211

postfixing, 209–210

prefixing, 209–210

* (dereferencing operator), 155–159

pointers, 171–172

/ (division operator), 100–101

== (equality operator), 217

compared to assignment operator, 218–220

++ (increment operator), 197, 207–208

pointers, 210–211

postfixing, 209–210

prefixing, 209–210

!= (inequality operator), 217

&& (logical AND operator), 262

alternative representations, 270

example, 263–265

precedence, 269–270

ranges, 265–267

! (logical NOT operator), 267–269

alternative representations, 270

precedence, 269

|| (logical OR operator), 260–262

alternative representations, 270

example, 261–262

-]* (member dereferencing operator), 1243–1246

.* (member dereferencing operator), 1243–1246

% (modulus operator), 101–102

* (multiplication operator), overloading, 574–578

\n newline character, 38–39

< operator, 217

. (period), 255

# (pound sign), 234

“ (quotation marks), 36

& (reference operator), 383–386

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги