[cfe-dev] Possible incompatible gcc attribute parsing with GCC

Eli Friedman eli.friedman at gmail.com
Fri Jul 1 01:18:02 PDT 2011


On Wed, Jun 29, 2011 at 2:32 PM, Xiaolong Tang <xiaolong.snake at gmail.com> wrote:
>
> Hi all,
>
> I am trying to see how far clang supports GNU attributes. It seems to
> me that clang does not support declare attributes for members of
> templates. Following is what I found:
>
> I use a very simple test code (in template_attribute.cpp):
>
>  template<typename T> class vector {
>  private:
>    T rep[10];
>  public:
>    T top() { return rep[0]; }
>  };
>
>  template<typename T> T vector<T>::top() __attribute__ ((pure));
>
>  int main()
>  {
>    vector<int> a;
>    int r = a.top();
>    int t = a.top();
>    return r + t;
>  }
>
> With this command:
>
>  g++ -O2 -cse -fno-inline -fdump-tree-all-all template_attribute.cpp
>
> the above code gets compiled and optimized (CSE works here).
>
> However, trying to compile the code with clang generates the following
> message:
>
>  16:15:55->clang++ -c template_attribute.cpp
>
>  template_attribute.cpp:9:35: error: out-of-line declaration of a member must be
>        a definition [-Wout-of-line-declaration]
>  template<typename T> T vector<T>::top() __attribute__ ((pure));
>                         ~~~~~~~~~~~^
>  template_attribute.cpp:9:35: warning: attribute declaration must precede
>        definition
>  template_attribute.cpp:5:5: note: previous definition is here
>    T top() { return rep[0]; }
>      ^
>  1 warning and 1 error generated.
>
> PS: clang version 2.9 (trunk 126848)

Like the error says, your template declaration is at the very least
strange... the more usual way to do what you're trying to do is to
change the definition to "__attribute__ ((pure)) T top() { return
rep[0]; }".

-Eli




More information about the cfe-dev mailing list