locked
warning C4227: anachronism used : qualifiers on reference are ignored Edit topicMark as RRS feed

  • Question

  • So I get that error when I try to do this:

    <var></var>
    typedef double const (*t_p_training_function)(std::vector<double> const&); // training function pointer type 

    The MS C++ docs say this about that error:
    <samp>anachronism used : qualifiers on reference are ignored
    Using qualifiers like const or volatile with C++ references is an outdated practice.</samp>
    Which I don't really understand. Why is using a const qualifier on a reference outdated? It's exactly what I want to do in this case (ie. I want a reference to it and I don't want to modify it).

    Should I just pass it as const? Will this have the same effect? Even so, what's wrong with the "reference" just for added clarity?
    Tuesday, April 2, 2013 3:22 AM

Answers

  • On 4/2/2013 8:35 PM, arman_sch wrote:

    Ah, I just figure out it was the line after that one. My apologies. Here is the line that is causing the error:

    std::vector<double>& const GetRandomProblem() const; // return a random problem vector

    That method returns a reference to a private member, which I want to return as constant. Is this not possible?

    const goes before or after the type, but certainly before the ampersand. So const std::vector<double>& or std::vector<double> const & - the two are equivalent.


    Igor Tandetnik

    • Marked as answer by Elegentin Xie Tuesday, April 9, 2013 9:52 AM
    Wednesday, April 3, 2013 12:38 AM

All replies

  • On 4/1/2013 11:22 PM, arman_sch wrote:

    So I get that error when I try to do this:

    typedef double const (*t_p_training_function)(std::vector<double> const&); // training function pointer type

    Which compiler are you using? I don't get any errors or warnings on this line, in VS2010 or VS2012.


    Igor Tandetnik

    Tuesday, April 2, 2013 4:01 AM
  • Ah, I just figure out it was the line after that one. My apologies. Here is the line that is causing the error:

    std::vector<double>& const GetRandomProblem() const; // return a random problem vector

    That method returns a reference to a private member, which I want to return as constant. Is this not possible?

    EDIT: The error resolved when I moved the ampersand:

    std::vector<double> const& GetRandomProblem() const; // return a random problem vector
    I didn't think it mattered where the ampersand was, I guess I was wrong!
    • Edited by arman_sch Wednesday, April 3, 2013 12:37 AM
    Wednesday, April 3, 2013 12:35 AM
  • On 4/2/2013 8:35 PM, arman_sch wrote:

    Ah, I just figure out it was the line after that one. My apologies. Here is the line that is causing the error:

    std::vector<double>& const GetRandomProblem() const; // return a random problem vector

    That method returns a reference to a private member, which I want to return as constant. Is this not possible?

    const goes before or after the type, but certainly before the ampersand. So const std::vector<double>& or std::vector<double> const & - the two are equivalent.


    Igor Tandetnik

    • Marked as answer by Elegentin Xie Tuesday, April 9, 2013 9:52 AM
    Wednesday, April 3, 2013 12:38 AM
  • // A reference to a constant integer
    int const & SomeNumber; 
    // OR 
    cont int & SomeNumber;
    
    // A constant reference to an integer
    int & const SomeNumber; 
    
    // A constant reference to a constant integer
    int const & const SomeNumber; 
    // OR 
    cont int & const SomeNumber;

    Granted, it is meaningless to have a constant reference, "type qualifier on return type is meaningless."
    • Edited by Sqandr Wednesday, October 16, 2013 5:09 PM
    Wednesday, October 16, 2013 5:05 PM
  • This answer helps to explain WHY this is so,  for more information, please refer to the 

    C++ FAQ "Clockwise Spiral Rule" entry

    Tuesday, March 26, 2019 6:50 PM