2010-10-08

どうも不思議なthread_localの仕様

thread_local指定子は、クラスのメンバーの際には、static指定子と共に宣言しなければならない。

void f()
{
    thread_local int x ; // 暗黙のうちにstatic
}

struct S
{
    // 宣言、staticも必要
    static thread_local int x ; 
} ;

// 定義、staticを使ってはいけない
thread_local int S::x ;

ここで、S::xの定義には、staticは使えない。なぜならば、staticデータメンバーの定義は名前空間スコープに書くわけで、そこでのstatic指定子の意味は、staticストレージではなく、internal linkageだからだ。

何故このような制限があるのか分からない。メンバーの宣言にも、暗黙のstaticを認めてもいいと思うのだが。第一、thread_localによって宣言された変数は、thread storagedである。static storageではない。

何故このような制限があるの変わらかないと書いたが、全然思い当たらないでもない。結局、static storageとは別に、static data memberという概念があるために、こうなっているのだろう。全然別の事を、ひとつのキーワードで行わせるのは、混乱のもとだと思う。

No comments:

Post a Comment

You can use some HTML elements, such as <b>, <i>, <a>, also, some characters need to be entity referenced such as <, > and & Your comment may need to be confirmed by blog author. Your comment will be published under GFDL 1.3 or later license with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.