模板静态成员初始化 [英] Template static member initialization

查看:89
本文介绍了模板静态成员初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我的模板类有一个const静态数据成员,由

构造函数使用。似乎该程序没有产生正确的

结果。要么它是编译器的错(gcc 4.1),要么我需要

了解更多关于静态成员的初始化...下面的代码

捕获问题并且是尽可能简单:


#include< iostream>

#include< limits>


template< typename Tstruct A {

const static unsigned NONE;

unsigned x;

A(){x = NONE; }

};


模板< typename Tconst unsigned A< T> :: NONE =

numeric_limits< unsigned> :: max();

A< doublea;


int main(void){

cout<< a.x<< " \ n";

cout<< A< double> :: NONE<< " \ n";

返回0;

}


该程序的输出是:

0

4294967295


在构建之前是否必须初始化?


干杯,

/ ALiX

Hi!

My template class has a const static data member, which is used by the
constructor. It seems that the program does not produce the correct
result. Either it is the fault of the compiler (gcc 4.1) or I need to
learn more about initialization of static members... The code below
captures the problem and is as simple as I could make it:

#include <iostream>
#include <limits>

template<typename Tstruct A {
const static unsigned NONE;
unsigned x;
A() { x = NONE; }
};

template<typename Tconst unsigned A<T>::NONE =
numeric_limits<unsigned>::max();
A<doublea;

int main(void) {
cout << a.x << "\n";
cout << A<double>::NONE << "\n";
return 0;
}

The output of the program is:
0
4294967295

Mustn''t NONE be initialized before a is constructed?

Cheers,
/ALiX

推荐答案

ALiX写道:
ALiX wrote:

我的模板类有一个const静态数据成员,由

构造函数使用。似乎该程序没有产生正确的

结果。要么它是编译器的错(gcc 4.1),要么我需要

了解更多关于静态成员的初始化...下面的代码

捕获问题并且是尽可能简单:


#include< iostream>

#include< limits>


template< typename Tstruct A {

const static unsigned NONE;

unsigned x;

A(){x = NONE; }

};


模板< typename Tconst unsigned A< T> :: NONE =

numeric_limits< unsigned> :: MAX();
My template class has a const static data member, which is used by the
constructor. It seems that the program does not produce the correct
result. Either it is the fault of the compiler (gcc 4.1) or I need to
learn more about initialization of static members... The code below
captures the problem and is as simple as I could make it:

#include <iostream>
#include <limits>

template<typename Tstruct A {
const static unsigned NONE;
unsigned x;
A() { x = NONE; }
};

template<typename Tconst unsigned A<T>::NONE =
numeric_limits<unsigned>::max();



std :: numeric_limits

std::numeric_limits


A< doublea;


int main(void){
A<doublea;

int main(void) {



删除''void'' - 糟糕的风格

Drop the ''void'' -- bad style


cout<< a.x<< " \ n";

cout<< A< double> :: NONE<< " \ n";

返回0;

}


该程序的输出是:

0

4294967295


在构建之前是否必须初始化?
cout << a.x << "\n";
cout << A<double>::NONE << "\n";
return 0;
}

The output of the program is:
0
4294967295

Mustn''t NONE be initialized before a is constructed?



它不是*实例化*除非它被使用。或许指的是

它在c-tor中没有实例化(因此确实没有得到初始化),但我认为它是编译器错误。 br />

尝试在课堂上初始化:


模板< typename Tstruct A {

static unsigned const NONE

= std :: numeric_limits< unsigned> :: max();

unsigned x;

A():x(NONE){}

};


V

-

请删除大写''A'当通过电子邮件回复

我没有回复最热门的回复,请不要问

It''s not *instantiated* unless it''s "used". Perhaps referring to
it in the c-tor does not get it instantiated (and therefore does
not get it initialised), but I would consider it a compiler bug.

Try initialising it right in the class:

template<typename Tstruct A {
static unsigned const NONE
= std::numeric_limits<unsigned>::max();
unsigned x;
A() : x(NONE) {}
};

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


7月26日,4日:下午50点,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
On Jul 26, 4:50 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

尝试在课堂上初始化:


template< ; typename Tstruct A {

static unsigned const NONE

= std :: numeric_limits< unsigned> :: max();

unsigned x;

A():x(无){}

};
Try initialising it right in the class:

template<typename Tstruct A {
static unsigned const NONE
= std::numeric_limits<unsigned>::max();
unsigned x;
A() : x(NONE) {}
};



然后我收到一个错误:

错误:''std :: numeric_limits< unsigned int> :: max()''不能出现在

常量表达式中

错误:函数调用不能出现在常量表达式中


const不能作为非const静态成员帮助

无法在类声明中初始化。


/ ALiX

Then I get an error:
error: ''std::numeric_limits<unsigned int>::max()'' cannot appear in a
constant-expression
error: a function call cannot appear in a constant-expression

Making NONE non-const does not help either as non-const static members
cannot be initialized in the class declaration.

/ALiX


这似乎有效。然而,它给了我另一个令人头痛的问题,因为在我正在编写的

实际应用程序中,NONE实际上是公开的(我应该在示例代码中将其公开为
。对不起!)。我使用它像

std :: string :: npos来返回''end-values''。我想要一个解决方案,

允许我保持NONE作为A级的公共静态成员。


干杯,

ALiX

This seems to work. However, it gives me another headache since in the
real application I''m writing, NONE is actually public (I should have
made it public in the example code... sorry!). I use it like
std::string::npos to return ''end-values''. I''d like a solution that
allows me to keep NONE as a public static member of the class A.

Cheers,
ALiX


这篇关于模板静态成员初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆