[llvm-bugs] [Bug 32557] New: thread_local data member of a class template is constructed on each access when the argument is a local structure

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Apr 6 15:30:31 PDT 2017


https://bugs.llvm.org/show_bug.cgi?id=32557

            Bug ID: 32557
           Summary: thread_local data member of a class template is
                    constructed on each access when the argument is a
                    local structure
           Product: clang
           Version: 4.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++11
          Assignee: unassignedclangbugs at nondot.org
          Reporter: shtaale at vivcourt.com
                CC: dgregor at apple.com, llvm-bugs at lists.llvm.org

Dear Sir/Madam,

This is a bug for Clang 4.0.0. The version 3.9.1  and earlier seem OK.
The source code working wrong:

#include <iostream>

struct A {
    int x = 1;
    A()   { std::cout << "A::A"  << std::endl; }
    ~A()  { std::cout << "A::~A" << std::endl; }
};

template<class T>
struct B {
    static thread_local A a;
};

template<class T>
thread_local A B<T>::a;

int main(void) {
    struct X {};
    std::cout << "main" << std::endl;
    int y = 0;
    for (int i = 0; i < 3; i++)
        y += B<X>::a.x;
    std::cout << "y: " << y << std::endl;
    std::cout << "main-exit" << std::endl;
}

It is expected to output:
main
A::A
y: 3
main-exit
A::~A

or
A::A
main
y: 3
main-exit
A::~A

Both would be OK from the standard PoV. The version 3.9.1 produces the 1st one.
But the version 4.0.0 produces:
main
A::A
A::A
A::A
y: 3
main-exit
A::~A
A::~A
A::~A
And this is very wrong. I.e. the constructor is called for the same object 3
times, followed by destructing the same object again 3 times.
Thanks

Regards
Alexander Shtannikov

Software developer
Vivienne Court Trading

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20170406/a09ccc65/attachment.html>


More information about the llvm-bugs mailing list