Bug 42840 - const-ref argument in a variadic template arglist is mishandled
Summary: const-ref argument in a variadic template arglist is mishandled
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.3.4
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-01-22 03:29 UTC by Navin Kumar
Modified: 2010-01-22 12:00 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
example of c++0x code that generates linker error (200 bytes, text/plain)
2010-01-22 03:32 UTC, Navin Kumar
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Navin Kumar 2010-01-22 03:29:59 UTC
When a static-const primitive is passed as a const-ref argument to function with a variadic template arglist, GCC generates code that references the static-const primitive, and optimizes away the static-const primitive, resulting in a "undefined reference" linker error.

/* Start of Example */
class Foo {
public:
    template <typename... T>
    static void foo(const T&... blah) { }
};

class Test {
public:
    static const int tmp = 1;
};

int main(int argc, char** argv) {
    Foo::foo(Test::tmp); // THIS COMPILES BUT LINKER FAILS
    //Foo::foo(static_cast<int>(Test::tmp)); // THIS COMPILES AND LINKS FINE
}
/* END OF EXAMPLE */

LINKER ERROR:
/tmp/ccfV1opu.o: In function `main':
tmp.cc:(.text+0x10): undefined reference to `Test::tmp'
collect2: ld returned 1 exit status

Since static cast of the static-const int primitive to an int fixes the problem, it's clear that GCC is somehow mishandling how it optimizes away Test::tmp even though it's being used as a const int& in Foo::foo(...)
Comment 1 Navin Kumar 2010-01-22 03:32:59 UTC
Created attachment 19686 [details]
example of c++0x code that generates linker error

compiled with ver 4.3.4 g++ and args -std=c++0x
Comment 2 Navin Kumar 2010-01-22 03:39:14 UTC
Ignore.  Close bug.

Found the issue here:
http://hellewell.homeip.net/phillip/blogs/index.php?entry=entry060314-000000
Comment 3 Jonathan Wakely 2010-01-22 12:00:46 UTC
N.B. duplicate of Bug 14404