operator[] compiler error and warning

Guido Ranzuglia

Probably it's cause i didn't sleep all the night...but can someone explain me why the first operator[] (the const one) generates a warning in MSVC 2010 saying that i'm returning the address of a temorary variable, and the second operator[] (the non-const one) produces a compiler error stating: 'return' : cannot convert from 'std::_Vb_reference<_Alloc>' to 'bool &' ?!?!?

Thanks a lot to everyone will waste time on my question.

    class ReqAtts
    {
    public:
        const bool& operator[](size_t ii) const
        {
            return _atts[ii];
        }

        bool& operator[](size_t ii)
        {
            return _atts[ii];
        }

    private:
        std::vector<bool> _atts;

    };
Dmitriy Zakablukov

std::vector<bool> is space efficient in most implementations (g++, MSVC): every item may be stored as a single bit, not as bool. You can read more here: en.cppreference.com

You have 2 options:

  1. Use std::vector<unsigned char> (or other integer type) instead of std::vector<bool>.
  2. Use std::vector<bool>::reference as return type for operator[] as follows:

    class ReqAtts
    {
    public:
        std::vector<bool>::const_reference operator[](size_t ii) const
        {
            return _atts[ii];
        }
    
        std::vector<bool>::reference operator[](size_t ii)
        {
            return _atts[ii];
        }
    
    private:
        std::vector<bool> _atts;
    
    };
    

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

why does operator < have a compiler error for Java generics?

分類Dev

Weird compiler warning about unused variable, is this a compiler bug?

分類Dev

CUDA compiler warning about unrecognized GCC pragma

分類Dev

Implicit operators and a compiler error

分類Dev

Java 1.8.20 Compiler error

分類Dev

Compiler error in .completePathIntoString

分類Dev

Odd Compiler Error in C++ (VC compiler)

分類Dev

Which gfortran compiler flag is associated with the warning "Illegal preprocessor directive"?

分類Dev

clang BlocksRuntime embeds 'obsolete compiler' warning in executable when using __block

分類Dev

Odd "implicit conversion" compiler warning in simple LINQ query

分類Dev

Dynamics ax 2012 compiler warning when seting smmOpportunityTable to common

分類Dev

MongoDB: $or operator error

分類Dev

MATLAB – Logarithm operator error

分類Dev

Is Homebrew's 'install' warning an error?

分類Dev

Compiler error appending slice to slice

分類Dev

Compiler error when throwing exception

分類Dev

Compiler error - Can not find symbol

分類Dev

warning C4146 minus operator on unsigned type

分類Dev

LINQ Equal Operator Error in Query

分類Dev

Compilation error: no match for 'operator--'

分類Dev

Syntax error using =~ operator in bash

分類Dev

ternary operator syntax error in class

分類Dev

Error using bit shift operator

分類Dev

Overloading * operator gives no match error

分類Dev

Code Analysis as Warning Locally but Error on TFS Build

分類Dev

implicit reinterpret cast on reference without warning/error

分類Dev

Warning: missing argument error on putting stores in database

分類Dev

WebGL: get error/warning message text as a string

分類Dev

Syntax error: invalid arithmetic operator (error token is ".")

Related 関連記事

  1. 1

    why does operator < have a compiler error for Java generics?

  2. 2

    Weird compiler warning about unused variable, is this a compiler bug?

  3. 3

    CUDA compiler warning about unrecognized GCC pragma

  4. 4

    Implicit operators and a compiler error

  5. 5

    Java 1.8.20 Compiler error

  6. 6

    Compiler error in .completePathIntoString

  7. 7

    Odd Compiler Error in C++ (VC compiler)

  8. 8

    Which gfortran compiler flag is associated with the warning "Illegal preprocessor directive"?

  9. 9

    clang BlocksRuntime embeds 'obsolete compiler' warning in executable when using __block

  10. 10

    Odd "implicit conversion" compiler warning in simple LINQ query

  11. 11

    Dynamics ax 2012 compiler warning when seting smmOpportunityTable to common

  12. 12

    MongoDB: $or operator error

  13. 13

    MATLAB – Logarithm operator error

  14. 14

    Is Homebrew's 'install' warning an error?

  15. 15

    Compiler error appending slice to slice

  16. 16

    Compiler error when throwing exception

  17. 17

    Compiler error - Can not find symbol

  18. 18

    warning C4146 minus operator on unsigned type

  19. 19

    LINQ Equal Operator Error in Query

  20. 20

    Compilation error: no match for 'operator--'

  21. 21

    Syntax error using =~ operator in bash

  22. 22

    ternary operator syntax error in class

  23. 23

    Error using bit shift operator

  24. 24

    Overloading * operator gives no match error

  25. 25

    Code Analysis as Warning Locally but Error on TFS Build

  26. 26

    implicit reinterpret cast on reference without warning/error

  27. 27

    Warning: missing argument error on putting stores in database

  28. 28

    WebGL: get error/warning message text as a string

  29. 29

    Syntax error: invalid arithmetic operator (error token is ".")

ホットタグ

アーカイブ