I was surprised to find the code below won't link, though both
compilers I have (g++ and aCC) give the same result so it's presumably
not a bug. Is this the problem, I've read elsewhere, about template
args having to have external linkage? But then why does the commented
out inline version work? Any help appreciated...
template <typename T> const T &max(const T &a, const T &b)
{
return (a > b ? a : b);
}
class C
{
public:
C(void); // THIS WORKS { int x = max(456, value); }
static const int value = 123;
};
C::C(void) { int x = max(456, value); } // THIS FAILS
int main(int, char *[]) { }
The linkage error is that it fails to resolve C::value.
--
Cheers,
-nick 5 1597
"Nick Battle" <ni*********@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com... I was surprised to find the code below won't link, though both compilers I have (g++ and aCC) give the same result so it's presumably not a bug. Is this the problem, I've read elsewhere, about template args having to have external linkage? But then why does the commented out inline version work? Any help appreciated...
template <typename T> const T &max(const T &a, const T &b) { return (a > b ? a : b); }
class C { public: C(void); // THIS WORKS { int x = max(456, value); }
static const int value = 123; };
You 'll need to provide a definition here:
const int C::value; C::C(void) { int x = max(456, value); } // THIS FAILS
int main(int, char *[]) { }
The linkage error is that it fails to resolve C::value.
-- Cheers, -nick
> You 'll need to provide a definition here: const int C::value;
That works, yes. Thanks.
But why was it happy with the inline constructor's use of the value?
--
Cheers,
-nick
I just tryed the code bellow on VC++ - it works like a charm. -
#include <iostream>
-
-
template <typename T> const T &max(const T &a, const T &b) { return (a
- b ? a : b); }
-
-
class A
-
{
-
public:
-
A(void);
-
static const int value = 123;
-
};
-
A::A(void) { int x = max(456, value); std::cout << "max(456," << value
-
<< ")=" << x << std::endl; }
-
-
class B
-
{
-
public:
-
B(void) { int x = max(456, value); std::cout << "max(456," << value <<
-
")=" << x << std::endl; }
-
static const int value = 123;
-
};
-
-
int main(int, char *[]) { A a; B b; return 0; }
-
I was surprised to find the code below won't link, though both compilers I have (g++ and aCC) give the same result so it's presumably not a bug. Is this the problem, I've read elsewhere, about template args having to have external linkage? But then why does the commented out inline version work? Any help appreciated...
template <typename T> const T &max(const T &a, const T &b) { return (a > b ? a : b); }
class C { public: C(void); // THIS WORKS { int x = max(456, value); }
static const int value = 123; };
C::C(void) { int x = max(456, value); } // THIS FAILS
int main(int, char *[]) { }
The linkage error is that it fails to resolve C::value.
-- Cheers, -nick
Hmmm... as I mentioned, g++ (3.3.4) and aCC (B3910B A.03.13), are both
the same and fail as I originally described.
OK. I just wondered whether I'd done something "obviously wrong".
Sounds like it's compiler dependent (so... maybe non-obviously wrong
:-)
Thanks for trying it.
--
Cheers,
-nick
In article <11**********************@z14g2000cwz.googlegroups .com>,
Nick Battle <ni*********@gmail.com> wrote: Hmmm... as I mentioned, g++ (3.3.4) and aCC (B3910B A.03.13), are both the same and fail as I originally described.
OK. I just wondered whether I'd done something "obviously wrong". Sounds like it's compiler dependent (so... maybe non-obviously wrong :-)
Thanks for trying it.
If you declare the ctor inline it might work with more compilers.
As is, it can delay the instantiation and by then doesn't know
about the constant as such. Independently, may also want to
inline max.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Nobody |
last post by:
This is sort of my first attempt at writing a template container class, just
wanted some feedback if everything looks kosher or if there can be any
improvements. This is a template class for a...
|
by: Steven T. Hatton |
last post by:
Basically the question is, can I do this?:
template <typename T>
class Vector3 {
public:
Vector3(const T& x=0,
const T& y=0,
const T& z=0)
: _v(3)
{
|
by: Erik Arner |
last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run
into all sorts of troubles that I'm sure depends on my lack of proper
understanding of C++. I would now like to get it right...
|
by: Gianni Mariani |
last post by:
The spirit of this arguably pointless exercise, is that the
numeric_limits<T> class could be replaced with a totally generic
template of compile-time, template computed constants.
The problem is...
|
by: Old Wolf |
last post by:
I tried this code:
#include <iostream>
#include <string>
template<typename T>
struct enum_properties
{
static const long max;
static const std::string name;
|
by: Serge Skorokhodov (216716244) |
last post by:
Hi,
I just seeking advice.
Some background information first because I've run into issues
that seems pretty obscure to me:( Quick search through KB yields
next to nothing. Simplified samples...
|
by: Markus Henschel |
last post by:
Hello,
this is a test case of something I just can't explain myself:
...
|
by: Pedro Sousa |
last post by:
Hi,
I'm trying to create an template class that represent points in all
possible dimensions, what I've made until now is
#ifndef _POINT_HPP_
#define _POINT_HPP_
#include <vector>
|
by: mast2as |
last post by:
Hi guys
Here's the class I try to compile (see below). By itself when I have a
test.cc file for example that creates an object which is an instance of
the class SpectralProfile, it compiles...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |