473,396 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Template args and static const members

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

Oct 14 '05 #1
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

Oct 14 '05 #2
> 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

Oct 14 '05 #3
I just tryed the code bellow on VC++ - it works like a charm.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. template <typename T> const T &max(const T &a, const T &b) { return (a
  4.  b ? a : b); }
  5.  
  6. class A
  7. {
  8. public:
  9. A(void);
  10. static const int value = 123;
  11. };
  12. A::A(void) { int x = max(456, value); std::cout << "max(456," << value
  13. << ")=" << x << std::endl; }
  14.  
  15. class B
  16. {
  17. public:
  18. B(void) { int x = max(456, value); std::cout << "max(456," << value <<
  19. ")=" << x << std::endl; }
  20. static const int value = 123;
  21. };
  22.  
  23. int main(int, char *[]) { A a; B b; return 0; }
  24.  
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


Oct 14 '05 #4
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

Oct 14 '05 #5
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?
Oct 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
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...
3
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) {
18
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...
5
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...
1
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;
3
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...
8
by: Markus Henschel | last post by:
Hello, this is a test case of something I just can't explain myself: ...
5
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>
5
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...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.