[system] Possible error in error_code.hpp

Hello, this file contains code to create an error condition, which basically looks like this: ---%<--- struct foo { foo() {} template <class T> foo(T t) { *this = make_foo(t); } }; inline foo make_foo(int i) { return foo(); } foo f(42); --->%--- Both my compiler (Tru64 CXX 7.1) and Comeau C++ Online think this code is in error: ---%<--- cxx: Error: test.cxx, line 4: identifier "make_foo" is undefined detected during instantiation of "foo::foo(T) [with T=int]" at line 9 template <class T> foo(T t) { *this = make_foo(t); } ----------------------------------------^ --->%--- Therefore I think this should be changed to the following: ---%<--- struct foo; template <class T> foo make_foo(T); struct foo { foo() {} template <class T> foo(T t) { *this = make_foo(t); } }; template<> foo make_foo(int i) { return foo(); } foo f(42); --->%--- Is this correct? If yes, could that header be fixed? TIA, Markus

Markus Schoepflin wrote:
Both my compiler (Tru64 CXX 7.1) and Comeau C++ Online think this code is in error:
You see the error because you compile in strict ansi mode which enables "strict dependent name lookup rules to be used in templates". It is the same with all EDG-based compilers: cxxosf.zko.hp.com> eccp -c --no_dep_name --version m.cpp Edison Design Group C/C++ Front End, version 3.5 (Dec 20 2004 13:57:48) Copyright 1988-2004 Edison Design Group, Inc. cxxosf.zko.hp.com> eccp -c --dep_name --version m.cpp Edison Design Group C/C++ Front End, version 3.5 (Dec 20 2004 13:57:48) Copyright 1988-2004 Edison Design Group, Inc. "m.cpp", line 4: error: identifier "make_foo" is undefined template <class T> foo(T t) { *this = make_foo(t); } ^ detected during instantiation of "foo::foo(T) [with T=int]" at line 9 1 error detected in the compilation of "m.cpp". cxxosf.zko.hp.com>
Therefore I think this should be changed to the following:
Another way of fixing it, the one that does not involve explicit specialization, can be defining foo's template ctor after make_foo() has been defined (or declared): ---%<--- struct foo { foo() {} template <class T> foo(T); }; inline foo make_foo(int i) { return foo(); } template <class T> foo::foo(T t) { *this = make_foo(t); } foo f(42); --->%--- Thanks, Boris ----- Original Message ----- From: "Markus Schöpflin" <markus.schoepflin@comsoft.de> To: <boost@lists.boost.org> Sent: Thursday, September 20, 2007 5:52 AM Subject: [boost] [system] Possible error in error_code.hpp
Hello,
this file contains code to create an error condition, which basically looks like this:
---%<--- struct foo { foo() {} template <class T> foo(T t) { *this = make_foo(t); } };
inline foo make_foo(int i) { return foo(); }
foo f(42); --->%---
Both my compiler (Tru64 CXX 7.1) and Comeau C++ Online think this code is in error:
---%<--- cxx: Error: test.cxx, line 4: identifier "make_foo" is undefined detected during instantiation of "foo::foo(T) [with T=int]" at line 9 template <class T> foo(T t) { *this = make_foo(t); } ----------------------------------------^ --->%---
Therefore I think this should be changed to the following:
---%<--- struct foo; template <class T> foo make_foo(T);
struct foo { foo() {} template <class T> foo(T t) { *this = make_foo(t); } };
template<> foo make_foo(int i) { return foo(); }
foo f(42); --->%---
Is this correct? If yes, could that header be fixed?
TIA, Markus
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Boris Gubenko wrote:
Another way of fixing it, the one that does not involve explicit specialization, can be defining foo's template ctor after make_foo() has been defined (or declared):
---%<--- struct foo { foo() {} template <class T> foo(T); };
inline foo make_foo(int i) { return foo(); }
template <class T> foo::foo(T t) { *this = make_foo(t); }
foo f(42); --->%---
I went with the fix from Markus because I thought it might be less prone to ordering failure for user supplied overloads. That's just speculation; I don't have easy access to a compiler I can test against. Thanks, --Beman

Markus Schöpflin wrote:
Hello,
this file contains code to create an error condition, which basically looks like this:
---%<--- struct foo { foo() {} template <class T> foo(T t) { *this = make_foo(t); } };
inline foo make_foo(int i) { return foo(); }
foo f(42); --->%---
Both my compiler (Tru64 CXX 7.1) and Comeau C++ Online think this code is in error:
---%<--- cxx: Error: test.cxx, line 4: identifier "make_foo" is undefined detected during instantiation of "foo::foo(T) [with T=int]" at line 9 template <class T> foo(T t) { *this = make_foo(t); } ----------------------------------------^ --->%---
Therefore I think this should be changed to the following:
---%<--- struct foo; template <class T> foo make_foo(T);
struct foo { foo() {} template <class T> foo(T t) { *this = make_foo(t); } };
template<> foo make_foo(int i) { return foo(); }
foo f(42); --->%---
Is this correct? If yes, could that header be fixed?
AFAICT you are correct. I've updated the header accordingly. I deliberately did not change error_code_user_test.cpp. It may require "template<>" be inserted in front of make_error_* overloads, but I want to let the test run through Tru64 CXX 7.1 to verify that before I change the test code. AFAIK, the code will still work OK with the compilers it is currently working with. Thanks for the report and fix! --Beman

Beman Dawes wrote: [...]
I deliberately did not change error_code_user_test.cpp. It may require "template<>" be inserted in front of make_error_* overloads, but I want to let the test run through Tru64 CXX 7.1 to verify that before I change the test code. AFAIK, the code will still work OK with the compilers it is currently working with.
Seems like the change is necessary. The linker complains about an unresolved make_error_code when linking. BTW, CXX gives some warnings about missing default constructors or initializers and I think it is right here, too. ---%<--- hp_cxx.compile.c++ ../../../bin.v2/libs/system/test/error_code_user_test.test/hp_cxx-71_006_tru64/debug/link-static/error_code_user_test.o cxx: Warning: error_code_user_test.cpp, line 130: const variable "boost::lib3::lib3_error_category_const" requires an initializer -- class "boost::lib3::lib3_error_category_imp" has no explicitly declared default constructor (D:misdefctocon) const lib3_error_category_imp lib3_error_category_const; ----------------------------------^ cxx: Warning: error_code_user_test.cpp, line 186: const variable "lib4::lib4_error_category_const" requires an initializer -- class "lib4::lib4_error_category_imp" has no explicitly declared default constructor (D:misdefctocon) const lib4_error_category_imp lib4_error_category_const; --------------------------------^ hp_cxx.link ../../../bin.v2/libs/system/test/error_code_user_test.test/hp_cxx-71_006_tru64/debug/link-static/error_code_user_test ld: Unresolved: boost::system::error_code boost::system::make_error_code<T0=boost::lib3::error>(T0) cxx -noimplicit_include -g -model ansi -version V7.1-006 -ieee -use_non_shared_libcxx -o "../../../bin.v2/libs/system/test/error_code_user_test.test/hp_cxx-71_006_tru64/debug/link-static/error_code_user_test" "../../../bin.v2/libs/system/test/error_code_user_test.test/hp_cxx-71_006_tru64/debug/link-static/error_code_user_test.o" "../../../bin.v2/libs/system/build/hp_cxx-71_006_tru64/debug/link-static/libboost_system-d-1_35.a" -lrt -lm --->%---
Thanks for the report and fix!
You're welcome. Markus
participants (3)
-
Beman Dawes
-
Boris Gubenko
-
Markus Schöpflin