Discussion:
Shall a variable template have the keyword static?
(too old to reply)
'Vlad from Moscow' via ISO C++ Standard - Discussion
2018-09-25 09:46:47 UTC
Permalink
According to the C++ 17 Standard ( 17 Templates, p. #3):

3. ...er a *concept-definition *or a *declaration *that defines a function,
a class, a variable, or a static
data member. A declaration introduced by a template declaration of a
variable is a *variable template*. A
variable template at class scope is a *static data member template*.
So in my opinion this means that the keyword static is not necessary when
declaring a variable template at class scope. However this code snippet
does not compile by gcc

namespace N
{
struct A
{
template <typename T>
T a;
};

template <typename T>
T A::a;
}

However if to explicitly specify the keyword static the code snippet does
compile.

namespace N
{
struct A
{
template <typename T>
static T a;
};


template <typename T>
T A::a;
}


What is wrong with the first code snippet relative to the quote from the
Standard?
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Nicolas Lesser
2018-09-25 09:48:04 UTC
Permalink
A non-static member of a class is not a variable according to the standard.

On Tue, Sep 25, 2018, 11:46 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
3. ...er a *concept-definition *or a *declaration *that defines a
function, a class, a variable, or a static
data member. A declaration introduced by a template declaration of a
variable is a *variable template*. A
variable template at class scope is a *static data member template*.
So in my opinion this means that the keyword static is not necessary when
declaring a variable template at class scope. However this code snippet
does not compile by gcc
namespace N
{
struct A
{
template <typename T>
T a;
};
template <typename T>
T A::a;
}
However if to explicitly specify the keyword static the code snippet does
compile.
namespace N
{
struct A
{
template <typename T>
static T a;
};
template <typename T>
T A::a;
}
What is wrong with the first code snippet relative to the quote from the
Standard?
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
'Vlad from Moscow' via ISO C++ Standard - Discussion
2018-09-25 09:51:23 UTC
Permalink
I'm sorry, I have not understood. Could you elaborate?

втПрМОк, 25 сеМтября 2018 г., 13:48:17 UTC+4 пПльзПватель Nicolas Lesser
Post by Nicolas Lesser
A non-static member of a class is not a variable according to the standard.
On Tue, Sep 25, 2018, 11:46 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
3. ...er a *concept-definition *or a *declaration *that defines a
function, a class, a variable, or a static
data member. A declaration introduced by a template declaration of a
variable is a *variable template*. A
variable template at class scope is a *static data member template*.
So in my opinion this means that the keyword static is not necessary
when declaring a variable template at class scope. However this code
snippet does not compile by gcc
namespace N
{
struct A
{
template <typename T>
T a;
};
template <typename T>
T A::a;
}
However if to explicitly specify the keyword static the code snippet does
compile.
namespace N
{
struct A
{
template <typename T>
static T a;
};
template <typename T>
T A::a;
}
What is wrong with the first code snippet relative to the quote from the
Standard?
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Nicolas Lesser
2018-09-25 10:28:08 UTC
Permalink
It says "a declaration introduced by a template declaration of a variable
is a variable template".

So:
template <typename>
int foo = 1; // variable template

But, a data member of a class is not a variable, so it is not a variable
template:

struct Foo {
template <typename>
int foo = 2; // not a variable => not a variable template => ill-formed
};

A variable template at class scope is also known as a static data member
template, because a variable template at class scope *has* to be a static
data member. A non-static data member is not a variable and as such not a
valid template declaration.

Hope that makes sense :)

On Tue, Sep 25, 2018, 11:51 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
I'm sorry, I have not understood. Could you elaborate?
втПрМОк, 25 сеМтября 2018 г., 13:48:17 UTC+4 пПльзПватель Nicolas Lesser
Post by Nicolas Lesser
A non-static member of a class is not a variable according to the standard.
On Tue, Sep 25, 2018, 11:46 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
3. ...er a *concept-definition *or a *declaration *that defines a
function, a class, a variable, or a static
data member. A declaration introduced by a template declaration of a
variable is a *variable template*. A
variable template at class scope is a *static data member template*.
So in my opinion this means that the keyword static is not necessary
when declaring a variable template at class scope. However this code
snippet does not compile by gcc
namespace N
{
struct A
{
template <typename T>
T a;
};
template <typename T>
T A::a;
}
However if to explicitly specify the keyword static the code snippet
does compile.
namespace N
{
struct A
{
template <typename T>
static T a;
};
template <typename T>
T A::a;
}
What is wrong with the first code snippet relative to the quote from the
Standard?
--
---
You received this message because you are subscribed to the Google
Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
'Vlad from Moscow' via ISO C++ Standard - Discussion
2018-09-26 15:01:48 UTC
Permalink
Well, then what does this code snipper in the Standard mean

struct matrix_constants {
template <class T>
using pauli = hermitian_matrix<T, 2>;
template <class T>
constexpr pauli<T> sigma1 = { { 0, 1 }, { 1, 0 } };
//...
};

Is sigma1 a static data member of the structure? If so why is the keyword
static absent?

втПрМОк, 25 сеМтября 2018 г., 13:28:20 UTC+3 пПльзПватель Nicolas Lesser
Post by Nicolas Lesser
It says "a declaration introduced by a template declaration of a variable
is a variable template".
template <typename>
int foo = 1; // variable template
But, a data member of a class is not a variable, so it is not a variable
struct Foo {
template <typename>
int foo = 2; // not a variable => not a variable template => ill-formed
};
A variable template at class scope is also known as a static data member
template, because a variable template at class scope *has* to be a static
data member. A non-static data member is not a variable and as such not a
valid template declaration.
Hope that makes sense :)
On Tue, Sep 25, 2018, 11:51 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
I'm sorry, I have not understood. Could you elaborate?
втПрМОк, 25 сеМтября 2018 г., 13:48:17 UTC+4 пПльзПватель Nicolas Lesser
Post by Nicolas Lesser
A non-static member of a class is not a variable according to the standard.
On Tue, Sep 25, 2018, 11:46 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
3. ...er a *concept-definition *or a *declaration *that defines a
function, a class, a variable, or a static
data member. A declaration introduced by a template declaration of a
variable is a *variable template*. A
variable template at class scope is a *static data member template*.
So in my opinion this means that the keyword static is not necessary
when declaring a variable template at class scope. However this code
snippet does not compile by gcc
namespace N
{
struct A
{
template <typename T>
T a;
};
template <typename T>
T A::a;
}
However if to explicitly specify the keyword static the code snippet
does compile.
namespace N
{
struct A
{
template <typename T>
static T a;
};
template <typename T>
T A::a;
}
What is wrong with the first code snippet relative to the quote from
the Standard?
--
---
You received this message because you are subscribed to the Google
Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Tom Honermann
2018-09-26 16:16:19 UTC
Permalink
On 09/26/2018 11:01 AM, 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
Well, then what does this code snipper in the Standard mean
struct matrix_constants {
    template <class T>
        using pauli = hermitian_matrix<T, 2>;
    template <class T>
        constexpr pauli<T> sigma1 = { { 0, 1 }, { 1, 0 } };
    //...
};
Is sigma1 a static data member of the structure? If so why is the
keyword static absent?
For reference, that code comes from http://eel.is/c++draft/temp#3.

This is a defect in the standard; the code above is ill-formed due to
the absence of 'static'.  There is no core issue for this yet that I'm
aware of, but it was recently discussed on the core mailing list.

Tom.
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
втПрМОк, 25 сеМтября 2018 г., 13:28:20 UTC+3 пПльзПватель Nicolas
It says "a declaration introduced by a template declaration of a
variable is a variable template".
template <typename>
int foo = 1; // variable template
But, a data member of a class is not a variable, so it is not a
struct Foo {
  template <typename>
  int foo = 2; // not a variable => not a variable template =>
ill-formed
};
A variable template at class scope is also known as a static data
member template, because a variable template at class scope *has*
to be a static data member. A non-static data member is not a
variable and as such not a valid template declaration.
Hope that makes sense :)
On Tue, Sep 25, 2018, 11:51 AM 'Vlad from Moscow' via ISO C++
I'm sorry, I have not understood. Could you elaborate?
втПрМОк, 25 сеМтября 2018 г., 13:48:17 UTC+4 пПльзПватель
A non-static member of a class is not a variable according
to the standard.
On Tue, Sep 25, 2018, 11:46 AM 'Vlad from Moscow' via ISO
3. ...er a /concept-definition /or a /declaration
/that defines a function, a class, a variable, or
a static
data member. A declaration introduced by a
template declaration of a variable is a /variable
template/. A
variable template at class scope is a /static data
member template/.
So in my opinion this means that the keyword static is
not necessary when declaring a variable template at
class scope. However this code snippet does not
compile by gcc
|
namespaceN
{
structA
{
template<typenameT>
    T a;
};
template<typenameT>
T A::a;
}
|
However if to explicitly specify the keyword static
the code snippet does compile.
|
namespaceN
{
structA
{
template<typenameT>
staticT a;
};
template<typenameT>
T A::a;
}
|
What is wrong with the first code snippet relative to
the quote from the Standard?
--
---
You received this message because you are subscribed
to the Google Groups "ISO C++ Standard - Discussion"
group.
To unsubscribe from this group and stop receiving
emails from it, send an email to
To post to this group, send email to
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/
<https://groups.google.com/a/isocpp.org/group/std-discussion/>.
--
---
You received this message because you are subscribed to the
Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/
<https://groups.google.com/a/isocpp.org/group/std-discussion/>.
--
---
You received this message because you are subscribed to the Google
Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Continue reading on narkive:
Loading...