Clang and C++Ox : what I'm doing wrong

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Clang and C++Ox : what I'm doing wrong

Jaonary Rabarisoa
Hi all,

I trying build the following simple code that use a static const variable inside a class but I got an undefinend reference error :

#include <iostream>

using namespace std;

class Test
{
  public :
    static const double value = 10.0;
};


int main()
{
  cout << "hello "  << Test::value << endl;
  return 0;
}

This code produce the following warning and error at link time :

clang: warning: argument unused during compilation: '-cc1'
/home/jrabarisoa/Sandbox/test_clang/main.cpp:27:25: warning: in-class initializer for static data member of type 'const double' is a
      C++0x extension [-Wc++0x-extensions]
    static const double value = 10.0;
                        ^       ~~~~
1 warning generated.
Linking CXX executable test
g++: unrecognized option '-cc1'
CMakeFiles/test.dir/main.cpp.o: In function `main':
/home/jrabarisoa/Sandbox/test_clang/main.cpp:(.text+0x7a): undefined reference to `Test::value'
collect2: ld returned 1 exit status

What I've missed ?

I use the latest clang from the svn repository.

Regards,

Jaonary

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Clang and C++Ox : what I'm doing wrong

Sebastian Redl

On 08.10.2010, at 09:27, Jaonary Rabarisoa wrote:

> Hi all,
>
> I trying build the following simple code that use a static const variable inside a class but I got an undefinend reference error :
>
> #include <iostream>
>
> using namespace std;
>
> class Test
> {
>   public :
>     static const double value = 10.0;
> };
> This code produce the following warning and error at link time :
>
> clang: warning: argument unused during compilation: '-cc1'
> /home/jrabarisoa/Sandbox/test_clang/main.cpp:27:25: warning: in-class initializer for static data member of type 'const double' is a
>       C++0x extension [-Wc++0x-extensions]

You're not actually compiling as C++0x.

>     static const double value = 10.0;
>                         ^       ~~~~
> 1 warning generated.
> Linking CXX executable test
> g++: unrecognized option '-cc1'
> CMakeFiles/test.dir/main.cpp.o: In function `main':
> /home/jrabarisoa/Sandbox/test_clang/main.cpp:(.text+0x7a): undefined reference to `Test::value'
> collect2: ld returned 1 exit status
>
> What I've missed ?

Since you didn't compile as C++0x, I can't be sure if the same issue exists there, but C++03 requires a definition of static class members unless it's an integral constant whose lvalue is never needed. I don't know if C++0x extends this to all literal type constants. I wouldn't be surprised if Clang requires the definition anyway.

Sebastian



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Clang and C++Ox : what I'm doing wrong

Argyrios Kyrtzidis-2
In reply to this post by Jaonary Rabarisoa
On Oct 8, 2010, at 12:27 AM, Jaonary Rabarisoa wrote:

> Hi all,
>
> I trying build the following simple code that use a static const variable inside a class but I got an undefinend reference error :
>
> #include <iostream>
>
> using namespace std;
>
> class Test
> {
>   public :
>     static const double value = 10.0;
> };
>
>
> int main()
> {
>   cout << "hello "  << Test::value << endl;
>   return 0;
> }
>
> This code produce the following warning and error at link time :
>
> clang: warning: argument unused during compilation: '-cc1'
> /home/jrabarisoa/Sandbox/test_clang/main.cpp:27:25: warning: in-class initializer for static data member of type 'const double' is a
>       C++0x extension [-Wc++0x-extensions]
>     static const double value = 10.0;
>                         ^       ~~~~
> 1 warning generated.
> Linking CXX executable test
> g++: unrecognized option '-cc1'
> CMakeFiles/test.dir/main.cpp.o: In function `main':
> /home/jrabarisoa/Sandbox/test_clang/main.cpp:(.text+0x7a): undefined reference to `Test::value'
> collect2: ld returned 1 exit status

Why are we requiring a definition for Test::value, isn't this supposed to be used as a constant ? John ?

-Argiris

>
> What I've missed ?
>
> I use the latest clang from the svn repository.
>
> Regards,
>
> Jaonary
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Clang and C++Ox : what I'm doing wrong

John McCall

On Oct 8, 2010, at 8:47 AM, Argyrios Kyrtzidis wrote:

> On Oct 8, 2010, at 12:27 AM, Jaonary Rabarisoa wrote:
>
>> Hi all,
>>
>> I trying build the following simple code that use a static const variable inside a class but I got an undefinend reference error :
>>
>> #include <iostream>
>>
>> using namespace std;
>>
>> class Test
>> {
>>  public :
>>    static const double value = 10.0;
>> };
>>
>>
>> int main()
>> {
>>  cout << "hello "  << Test::value << endl;
>>  return 0;
>> }
>>
>> This code produce the following warning and error at link time :
>>
>> clang: warning: argument unused during compilation: '-cc1'
>> /home/jrabarisoa/Sandbox/test_clang/main.cpp:27:25: warning: in-class initializer for static data member of type 'const double' is a
>>      C++0x extension [-Wc++0x-extensions]
>>    static const double value = 10.0;
>>                        ^       ~~~~
>> 1 warning generated.
>> Linking CXX executable test
>> g++: unrecognized option '-cc1'
>> CMakeFiles/test.dir/main.cpp.o: In function `main':
>> /home/jrabarisoa/Sandbox/test_clang/main.cpp:(.text+0x7a): undefined reference to `Test::value'
>> collect2: ld returned 1 exit status
>
> Why are we requiring a definition for Test::value, isn't this supposed to be used as a constant ? John ?

Probably various places don't realize that they can emit that as a constant.

John.


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Clang and C++Ox : what I'm doing wrong

Argyrios Kyrtzidis-2
On Oct 8, 2010, at 8:49 AM, John McCall wrote:

>
> On Oct 8, 2010, at 8:47 AM, Argyrios Kyrtzidis wrote:
>
>> On Oct 8, 2010, at 12:27 AM, Jaonary Rabarisoa wrote:
>>
>>> Hi all,
>>>
>>> I trying build the following simple code that use a static const variable inside a class but I got an undefinend reference error :
>>>
>>> #include <iostream>
>>>
>>> using namespace std;
>>>
>>> class Test
>>> {
>>> public :
>>>   static const double value = 10.0;
>>> };
>>>
>>>
>>> int main()
>>> {
>>> cout << "hello "  << Test::value << endl;
>>> return 0;
>>> }
>>>
>>> This code produce the following warning and error at link time :
>>>
>>> clang: warning: argument unused during compilation: '-cc1'
>>> /home/jrabarisoa/Sandbox/test_clang/main.cpp:27:25: warning: in-class initializer for static data member of type 'const double' is a
>>>     C++0x extension [-Wc++0x-extensions]
>>>   static const double value = 10.0;
>>>                       ^       ~~~~
>>> 1 warning generated.
>>> Linking CXX executable test
>>> g++: unrecognized option '-cc1'
>>> CMakeFiles/test.dir/main.cpp.o: In function `main':
>>> /home/jrabarisoa/Sandbox/test_clang/main.cpp:(.text+0x7a): undefined reference to `Test::value'
>>> collect2: ld returned 1 exit status
>>
>> Why are we requiring a definition for Test::value, isn't this supposed to be used as a constant ? John ?
>
> Probably various places don't realize that they can emit that as a constant.

Jaonary, please file a bug report so we can keep track of this.

-Argiris

>
> John.
>
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Clang and C++Ox : what I'm doing wrong

James Noble
In reply to this post by Argyrios Kyrtzidis-2

On Oct 8, 2010, at 11:47 AM, Argyrios Kyrtzidis wrote:

>
> Why are we requiring a definition for Test::value, isn't this  
> supposed to be used as a constant ? John ?
>
> -Argiris

I think the C++ standard mandates that even a static const variable  
must have a
definition.  I think some compilers allow integral types to skirt  
around that but
I think this is why there is the "enum hack" for C++ ... the following  
is a link
to an article that describes the enum hack

http://www.drdobbs.com/184403277

James


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Clang and C++Ox : what I'm doing wrong

John McCall
On Oct 8, 2010, at 9:14 AM, James Noble wrote:
On Oct 8, 2010, at 11:47 AM, Argyrios Kyrtzidis wrote:
Why are we requiring a definition for Test::value, isn't this supposed to be used as a constant ? John ?

I think the C++ standard mandates that even a static const variable must have a
definition.  I think some compilers allow integral types to skirt around that but
I think this is why there is the "enum hack" for C++ ...

This is not quite correct.  The standard says that a definition must given for a static const literal-typed data member if it is used in the program.  "Used" is actually a term of art from [basic.def.odr]p2:
  A variable or non-overloaded function whose name appears as a potentially-evaluated expression is used unless it is an object that satisfies the requirements for appearing in a constant expression and the lvalue-to-rvalue conversion is immediately applied.
So this is actually a point of non-compliance for us with C++0x, which is okay because we don't actually claim to support C++0x.  Nonetheless, we should implement this.

The enum hack is useful mostly because it's easy to accidentally bind a reference to a const static data member and thus "use" it according to the standard — usually because some template function takes a 'const T &' (to avoid unnecessary copies of aggregate arguments) and then 'T' gets instantiated with 'int' and suddenly you're binding to the variable instead of a temporary.  Never let it be said that C++ is not an awful language.

John.

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev