-1

I have some trouble compiling a piece of C++ code using Clang 3.3 on linux. However the same piece of code compiles with gcc 4.8.2 as well as Intel Compiler. So I wanted if my code is actually legal. Usually I trust clang more with such questions ;)

Anyway, so here is the code fragment:

namespace test {
   template<class SCALAR=double>
    struct Foo {
     public:
      template<class SCALAR_ARG>
      friend Foo<SCALAR_ARG> create_Foo( );

      typedef SCALAR scalar_t;
    };

    template<class SCALAR_ARG=double>
    Foo<SCALAR_ARG> create_Foo( )
    {
      typedef Foo<SCALAR_ARG> impl_t;
      return impl_t();
    }

}

struct Dummy {
  typedef Dummy impl_t;
};

int main() {
  typedef test::Foo<Dummy> foo_t;
  typedef typename foo_t::scalar_t scalar_t;

  Dummy  egv_;
  test::create_Foo();

  return 0;
}

What do you think? Should I post it as bug in Clang or is it actually ill-formed?

Thanks in Advance, Raffael

craffael
  • 373
  • 1
  • 10

1 Answers1

0
  1. It would be good to see what error you actually get with clang.

  2. You have a lot of noise in the code you posted, parts that are unrelated to the problem.

  3. Here is how you should deal with template friends.

Following the advice given there, here is how I fixed your code:

namespace test {

  template <class > struct Foo;

  template <class T=double> Foo<T> create_Foo();

  template<class SCALAR=double>
    struct Foo {
    public:

      friend Foo create_Foo<>( );

      typedef SCALAR scalar_t;
    };

    template<class SCALAR_ARG>
    Foo<SCALAR_ARG> create_Foo( )
    {
      typedef Foo<SCALAR_ARG> impl_t;
      return impl_t();
    }

}

struct Dummy {
  typedef Dummy impl_t;
};

int main() {
  typedef test::Foo<Dummy> foo_t;
  typedef typename foo_t::scalar_t scalar_t;

  Dummy  egv_;
  test::create_Foo();

  return 0;
}

It compiles both with gcc 4.7.2 and clang++ 3.4 (trunk).

Ali
  • 56,466
  • 29
  • 168
  • 265