[cfe-dev] time of inline assembler evaluation in template specialization

John McCall rjmccall at apple.com
Thu Jun 23 09:45:53 PDT 2011


On Jun 22, 2011, at 11:52 PM, Titus von Boxberg wrote:
> I tried to use template specialization for selecting
> the right inline assembly code.
> 
> Example:
> -----------------
> template<int CPU>
> struct A {
> };
> 
> template<>
> struct A<8> {
>  void f(void) {
>    asm("ldr r4,=1\n\t" ::: "r4");
>  }
> };
> 
> template<>
> struct A<1> {
>  void f(void) {
>    asm("movl $1, %%eax\n\t" ::: "eax");
>  }
> };
> 
> 
> int main(void)
> {
> A<1>	a;
> a.f();
> }
> ---------------
> clang says:
> 
> error: unknown register name 'r4' in asm
>   asm("ldr r4,=1\n\t" ::: "r4");
> 
> g++ compiles the code, both for arm (A<8>) and x86.
> 
> Even if the register name r4 in the clobber list would really
> be invalid, I'd expect that the compiler would only analyze
> the syntactical correctness of the asm statement itself while
> parsing the template.
> Otherwise this application of template specialization cannot work.
> 
> Is this assumption correct?

No.  A program is ill-formed, no diagnostic required, if it contains
a template specialization with no valid instantiations.  You cannot
rely on (non-dependent) code in dead template specializations
to not be checked for validity.

John.



More information about the cfe-dev mailing list