Quantcast
Channel: Intel® C++ Compiler
Viewing all articles
Browse latest Browse all 1616

[Bug Report] miscompiles static locals of function template instantiations icpc (ICC) 15.0.3 20150407

$
0
0

Using boost::function with functors defined in local namespaces of TUs is currently broken if the functor is a class that has the same name in multiple TUs, because boost::function uses static locals in its implementations. We were able to reduce the problem to the following testcase

a1.cc:

    template<typename T>
    int f(T) { static int x = T::x; return x; }

    namespace { struct A { static const int x = 1; }; }
   
    int f1() {
       return f(A());
    }

a2.cc:

    template<typename T>
    int f(T) { static int x = T::x; return x; }
    
    namespace { struct A { static const int x = 0; }; }
    
    int f2() {
       return f(A());
    }

main.cc:

    #include <cstdio>
    
    int f1();
    int f2();
    
    int main() {
       std::printf("%d != %d\n", f1(), f2());
    } 

Command line:

    # icpc a1.cc a2.cc main.cc -o main
    # ./main
    0 != 0

When inspecting the produced symbols with readelf, we noted that while `f` is has local linkage, as we think it should have, the `x` static variable receives weak linkage and gets the same mangled name and therefore the two `x`'es are merged and it becomes lottery which is chosen.
    
    # icpc a2.cc a1.cc main.cc -o main
    # ./main
    1 != 1

GCC compiles this and produces the expected output. I also asked on Stackoverflow about it, see http://stackoverflow.com/questions/33262590/are-static-locals-of-functio...


Viewing all articles
Browse latest Browse all 1616

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>