Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

About static const member in template class

45 views
Skip to first unread message

JiiPee

unread,
Jun 17, 2015, 7:58:06 PM6/17/15
to
I would like to have a static integer member in a template class without
the need to call it with template parameters:

template <typename T>
class A
{
public:
A(const T& data, int nID = ID_NOT_IN_USE) : // copy constructor
m_data(data), m_ID(nID)
{ }

static const int ID_NOT_IN_USE = -1;

T data;
int m_ID;
};

This works, but to call ID_NOT_IN_USE I have to give a type even though
I just want to know the value of ID_NOT_IN_USE:
cout<<A<int>::ID_NOT_IN_USE;
Is it possible to create a static member which does require the type
when it is used? I know A::ID_NOT_IN_USE does not work, but is there any
other way? What is the best way to solve this as I would like to keep
ID_NOT_IN_USE to be a member of A or at least dependent of it somehow
because it obviously belongs to class A. I know I can make it a global :

const int ID_NOT_IN_USE = -1;
template <typename T>
class A
{ ...

but now its not anymore dependent of A.

Sam

unread,
Jun 17, 2015, 8:41:40 PM6/17/15
to
Right. And if it's dependent on A, you have to reference it as a class
member.

This looks like an XY problem. What actual problem are you trying to solve.
No, not what you wrote, but the real problem that you think this is a
solution for.

Victor Bazarov

unread,
Jun 17, 2015, 8:48:52 PM6/17/15
to
On 6/17/2015 7:57 PM, JiiPee wrote:
> I would like to have a static integer member in a template class without

There is no such thing as "a template class".
You keep writing "class A", "belongs to class A", but there is no such
thing! "A" is not a class! It's a class *template*. There is no
class, there is no member, until you *instantiate* that template, i.e.
give it a certain set of arguments.

What prevents me from doing

template<> class A<std::string> {
unsigned ID_NOT_IN_USE = 666;
};

? Nothing! It's called "specialization" and you have no control over
what I stick into that *class* and how.

I strongly recommend defining an actual *class* (not a template),
putting your important static constant in it, and then deriving your
template from that class and bringing your const name into it by means
of the 'using' declaration:

struct HasSpecialConst {
static const int ID_NOT_IN_USE = -1;
};

template<class T> class A : HasSpecialConst {
...
using HasSpecialConst::ID_NOT_IN_USE;
};

(let us know if it doesn't work for you, and why).

V
--
I do not respond to top-posted replies, please don't ask

Ed Anson

unread,
Jun 17, 2015, 8:56:32 PM6/17/15
to
When creating a template class having members that do not depend on a
template parameter, I typically put those members in a non-template
class that is inherited by the template. Although that might be a bit
verbose in this case, it would work. Of course, you still have to refer
to a class name (when referencing it outside the class) but at least you
don't need to specify a template parameter. Of course, ANY reference to
a member from outside the class requires mention of either an instance
or a class name, depending on whether the member is static.

Message has been deleted

Victor Bazarov

unread,
Jun 18, 2015, 7:36:31 AM6/18/15
to
On 6/17/2015 9:46 PM, Stefan Ram wrote:
> Victor Bazarov <v.ba...@comcast.invalid> writes:
>> On 6/17/2015 7:57 PM, JiiPee wrote:
>>> I would like to have a static integer member in a template class without
>> There is no such thing as "a template class".
>
> No. But there is a rule that whenever someone posts
> »There is no ...«, you can find this »...« /somewhere/ in C++.
> The is a side-effect of the sheer size of C++, one might
> call it: size-effect.
>
> 23.3.1p1:
>
> The headers <array>, <deque>, <forward_list>, <list>,
> and <vector> define template classes«
>
> 14.6.5p2:
>
> »As with non-template classes, the names of namespace ...«
>
> 14.7.3p7:
>
> »member templates of non-template classes«

Those are glaring defects in the Standard. Good that you've found them.

Öö Tiib

unread,
Jun 20, 2015, 8:52:18 PM6/20/15
to
Yes "template class" does not make sense but "non-template class" makes
sense to me. Or ... how else to call the latter?

Ian Collins

unread,
Jun 20, 2015, 8:58:29 PM6/20/15
to
A class? Or if you must be specific, a concrete class

--
Ian Collins

Öö Tiib

unread,
Jun 20, 2015, 10:26:59 PM6/20/15
to
I meant in contexts of standard where it for some reason wants to
emphasize that it is said about ordinary class not about some class
template or class template instantiation or class template specialization.
It is defect to call it "non-template class"?

JiiPee

unread,
Jun 21, 2015, 9:26:36 AM6/21/15
to
On 18/06/2015 01:48, Victor Bazarov wrote:
>
> struct HasSpecialConst {
> static const int ID_NOT_IN_USE = -1;
> };
>
> template<class T> class A : HasSpecialConst {
> ...
> using HasSpecialConst::ID_NOT_IN_USE;
> };
>
> (let us know if it doesn't work for you, and why).
>
> V

ok thanks. I think Ed said the same thing. I will try this and see how
it goes...
its a good ideat to try at least

JiiPee

unread,
Jun 21, 2015, 9:27:01 AM6/21/15
to
ok, Victor said the same thing. I will try this and see how it goes

JiiPee

unread,
Jun 21, 2015, 9:39:12 AM6/21/15
to
this is about saving the ID of the object into a file. Just interested
how this can be done correctly. others already suggested to create a
base class.

JiiPee

unread,
Jun 21, 2015, 10:29:36 AM6/21/15
to
On 18/06/2015 01:48, Victor Bazarov wrote:
>
> struct HasSpecialConst {
> static const int ID_NOT_IN_USE = -1;
> };
>
> template<class T> class A : HasSpecialConst {
> ...
> using HasSpecialConst::ID_NOT_IN_USE;
> };
>
> (let us know if it doesn't work for you, and why).
>
> V

Thanks. This works! And, there is no need to create a new base class as
my class already has a base class, so I just moved the static const int
ID_NOT_IN_USE = -1; there as you pointed out. Learned a new thing :)

JiiPee

unread,
Jun 21, 2015, 10:30:04 AM6/21/15
to
On 18/06/2015 01:56, Ed Anson wrote:
I answered to Victor, this worked!
Message has been deleted

Öö Tiib

unread,
Jun 21, 2015, 8:28:00 PM6/21/15
to
On Sunday, 21 June 2015 19:19:17 UTC+3, Stefan Ram wrote:
> Öö Tiib <oot...@hot.ee> writes:
> >I meant in contexts of standard where it for some reason wants to
> >emphasize that it is said about ordinary class not about some class
> >template or class template instantiation or class template specialization.
> >It is defect to call it "non-template class"?
>
> Scott Meyers wrote that he sometimes refers to »classes«
> that are really templates and it is his opinion that such
> »sloppiness« hurts no one as long as one can understand it.
>
> However, in the case of a technical specification (or even a
> beginner's tutorial), a controlled vocabulary might be
> better. For example, I have problems understanding the rules
> for initialization because they depend on which constructors
> are »there« (that is, user-defined, implicit, defaulted,
> deleted, or nonexistend default constructors - these are all
> different cases! And I might still have missed some) and it
> seems to be that they sometimes used different wording for
> the same thing, (possibly »user-defined« = »explicit« or so).
> And this makes it even harder to read.

One reason why the standard is hard to read is that it is meant
as specification for writing C++ compilers and standard libraries.
Specification and user's manual are usually different things.

Other reason is that it is quite rapid voluntary work of many
unrelated people on rather complex subject. The result of such
work usually contains some controversies or inconsistencies.
0 new messages