Gcc Error Reference Cannot Be Declared Mutable
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting
Error C2178 Cannot Be Declared With Mutable Specifier
ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the c++ mutable Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Why can't I declare a reference to a mutable object? (“reference cannot be declared mutable”) up vote 18 down vote favorite 1 Let's say we have a test.cpp as follows: class A; class B { private: A mutable& _a; }; Compilation: $> gcc test.cpp test.cpp:6:20: error: reference ‘_a’ cannot be declared ‘mutable’ [-fpermissive] My gcc: $> gcc --version gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 Copyright (C) 2011 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Why? c++ reference share|improve this question edited Dec 12 '11 at 2:26 asked Dec 12 '11 at 2:11 paps 9517 Are you looking for a pointer? –Joe McGrath Dec 12 '11 at 2:14 Even if you could do this it would be useless because C++ does not contain any syntax that can change a reference. You can't even get the memory address of a reference. If you try you get the address of the object that the reference points to. –bames53 Dec 12 '11 at 2:23 Sorry, I asked the wrong question. This is not a mutable reference, this is a reference to a mutable object! (I changed the title) –paps Dec 12 '11 at 2:27 add a comment| 4 Answers 4 active oldest votes up vote 27 down vote accepted There is no reason to have a reference member mutable. Why? Because const member functions can change the object which is referenced by a class member: class B { public: B(int var) : n(var) {}; void Set(int val) const { n = val; } //no error void SetMember(int val) const { m = val; } //error assignment of member `B::m' in read-only structure protected: int& n; int m; }; share|improve this answer answered Dec 12 '11 at 2:58 Pavel Zhuravlev 1,6201913 Thank you! This is the answer I was looking for. –paps Dec 12 '11 at 14:58 add a comment| up vote 8 down vote References can only be assigned when constructing an object, and cannot be modified thereafter. Thus making them mutable would have no meaning, which is why the standard disallows it. sh
User account creation filtered due to spam. Bug33558 - 'mutable' incorrectly accepted on reference members Summary: 'mutable' incorrectly accepted on reference members Status: RESOLVED FIXED Alias: None Product: gcc Classification: Unclassified Component: c++ (show other bugs) Version: 4.1.2 Importance: P3 normal Target Milestone: 4.6.0 Assignee: Not yet assigned to anyone URL: Keywords: accepts-invalid, patch Depends on: Blocks: Reported: 2007-09-26 03:13 UTC by Wolfgang Bangerth Modified: 2011-01-15 14:42 UTC (History) CC List: 3 users http://stackoverflow.com/questions/8469209/why-cant-i-declare-a-reference-to-a-mutable-object-reference-cannot-be-decla (show) fang gafunchal gcc-bugs See Also: Host: Target: Build: Known to work: Known to fail: Last reconfirmed: 2008-12-31 17:59:36 Attachments patch (412 bytes, patch) 2010-01-09 14:18 UTC, Giovanni Funchal Details | Diff View All Add an attachment (proposed patch, testcase, etc.) Note You need to log in before you can comment on or make changes to https://gcc.gnu.org/PR33558 this bug. Description Wolfgang Bangerth 2007-09-26 03:13:14 UTC 7.1.1/8 says that 'mutable' can't be applied to reference members: The mutable specifier can be applied only to names of class data members (_class.mem_) and cannot be applied to names declared const or static, and cannot be applied to reference members. Yet gcc accepts this code: ---------------- class X { mutable int &q; }; ---------------- This would seem to be in error. W. Comment 1 Andrew Pinski 2008-12-31 17:59:36 UTC Confirmed. Comment 2 Giovanni Funchal 2010-01-09 14:18:02 UTC Created attachment 19523 [details] patch Comment 3 Giovanni Funchal 2010-01-09 14:18:54 UTC http://gcc.gnu.org/ml/gcc-patches/2010-01/msg00420.html -- Giovanni Comment 4 Jonathan Wakely 2010-12-20 09:43:40 UTC new patch http://gcc.gnu.org/ml/gcc-patches/2010-12/msg01469.html Comment 5 Jonathan Wakely 2011-01-15 14:41:12 UTC Author: redi Date: Sat Jan 15 14:41:09 2011 New Revision: 168843 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=168843 Log: 2011-01-15 Giovanni Funchal
you by: ericehlers, lballabio, nando Summary Files Reviews Support Mailing Lists Tickets ▾ Patches Bugs News Git repository Old code Create Ticket View Stats Group Searches https://sourceforge.net/p/quantlib/bugs/131/ Changes Closed Tickets Open Tickets Help Formatting Help #131 g++ complains about mutable variables Status: closed-duplicate Owner: nobody Labels: None Priority: 5 Updated: 2011-06-23 Created: 2011-05-26 Creator: charas Private: http://www.k-3d.org/node/800 No While building a package for quantlib on archlinux i got the following warning and error reference 'y2_' cannot be declared 'mutable' [-fpermissive] ../ql/math/interpolations/multicubicspline.hpp:277:34: warning: reference 'v_' cannot cannot be be declared 'mutable' [-fpermissive] ../ql/math/interpolations/multicubicspline.hpp:336:33: warning: reference 'y2_' cannot be declared 'mutable' [-fpermissive] ../ql/math/interpolations/multicubicspline.hpp:337:34: warning: reference 'v_' cannot be declared 'mutable' [-fpermissive] ../ql/math/interpolations/multicubicspline.hpp:337:39: warning: reference 'v1_' cannot be declared 'mutable' [-fpermissive] ../ql/math/interpolations/multicubicspline.hpp:337:45: warning: reference 'v2_' cannot be declared 'mutable' [-fpermissive] make[2]: *** [BermudanSwaption.o] Error 1 After compiling with -fpermissive I could get it compiled but the warnings are cannot be declared still there whenever the compiler encounters the mutable variables. Thanks for Quantlib Discussion Fahad Khan - 2011-06-21 I don't get any such issues on Fedora 15. Both on QuantLib-1.1 and trunk $ gcc --version gcc (GCC) 4.6.0 20110509 (Red Hat 4.6.0-7) If you would like to refer to this comment somewhere else in this project, copy and paste the following link: Hong Yu - 2011-06-22 I will be interested to help; however, I currently do not have sufficient computer space to install Fedora. So if you have Fedora and can let me access via SSH/SFTP etc., I will be happy to help tracing. My contact is:
- 19:50 #1 ForumImport Offline Last seen: 7 years 3 weeks ago Joined: 09/22/2009 - 18:47 Gentoo gcc-4.6.1 and boost 1.46 problem [ 15%] Building CXX object k3dsdk/CMakeFiles/k3dsdk.dir/xpath.cpp.o In file included from /var/tmp/portage/media-gfx/k3d-0.8.0.1/work/k3d-source-0.8.0.1/k3dsdk/xpath.cpp:28:0: /usr/include/boost-1_46/boost/spirit.hpp:18:4: warning: #warning "This header is deprecated. Please use: boost/spirit/include/classic.hpp" [-Wcpp] In file included from /var/tmp/portage/media-gfx/k3d-0.8.0.1/work/k3d-source-0.8.0.1/k3dsdk/gil/boost/gil/extension/numeric/../../extension/dynamic_image/apply_operation.hpp:21:0, from /var/tmp/portage/media-gfx/k3d-0.8.0.1/work/k3d-source-0.8.0.1/k3dsdk/gil/boost/gil/extension/numeric/../../extension/dynamic_image/dynamic_image_all.hpp:24, from /var/tmp/portage/media-gfx/k3d-0.8.0.1/work/k3d-source-0.8.0.1/k3dsdk/gil/boost/gil/extension/numeric/resample.hpp:14, from /var/tmp/portage/media-gfx/k3d-0.8.0.1/work/k3d-source-0.8.0.1/k3dsdk/utility_gl.cpp:36: /var/tmp/portage/media-gfx/k3d-0.8.0.1/work/k3d-source-0.8.0.1/k3dsdk/gil/boost/gil/extension/numeric/../../extension/dynamic_image/apply_operation_base.hpp:117:22: error: reference ‘_op’ cannot be declared ‘mutable’ [-fpermissive] /var/tmp/portage/media-gfx/k3d-0.8.0.1/work/k3d-source-0.8.0.1/k3dsdk/gil/boost/gil/extension/numeric/../../extension/dynamic_image/apply_operation_base.hpp:130:22: error: reference ‘_op’ cannot be declared ‘mutable’ [-fpermissive] make[2]: *** [k3dsdk/CMakeFiles/k3dsdk.dir/utility_gl.cpp.o] Error 1 make[1]: *** [k3dsdk/CMakeFiles/k3dsdk.dir/all] Error 2 make: *** [all] Error 2 emake failed Top Log in or register to post comments Mon, 09/05/2011 - 20:26 #2 ForumImport Offline Last seen: 7 years 3 weeks ago Joined: 09/22/2009 - 18:47 Gentoo gcc-4.6.1 and boost 1.46 problem Could be more gcc 4.6 related than boost 1.46, I'm using boost 1.46.1 without problems here, though with gcc 4.5. Would it be much trouble to try with the latest mercurial? If you have the dependencies installed already, the quick instructions at http://developer.k-3d.org/ should work. Regards, Bart Top Log in or register to post comments Fri, 09/09/2011 - 20:32 #3 ForumImport Offline Last seen: 7 years 3 weeks ago Joined: 09/22/2009 - 18:47 Gentoo gcc-4.6.1 and boost 1.46 problem Aha, I see. Thank you. Tried fresh sources and got different error much mor later: [ 42%] Building CXX object modules/nurbs/CMakeFiles/k3d-nurbs.dir/extrude_patch.cpp.o In file included from /usr/include/boost-1_46/boost/gil/extension/io/png_io.hpp:41:0, from