Home out-of-line constructor template works in GCC fails in Clang
Reply: 0

out-of-line constructor template works in GCC fails in Clang

user1348
1#
user1348 Published in May 22, 2018, 9:19 pm

The following code works fine in GCC, but fails to compile in clang:

#include <iostream>
#include <string>

template <typename T>
struct A {
template <typename C>
  A(const C& c) { std::cout << "base" << std::endl; }
};

template <>
template <>
A<std::string>::A<std::string>(const std::string& s) { 
  std::cout << s << std::endl; 
}

int main()
{
  std::string f("foo");
  A<std::string> a(f);
  A<std::string> b(1.2);
}

GCC outputs:

foo
base

But Clang gives the following error:

source_file.cpp:14:17: error: out-of-line constructor for 'A' cannot have 
template arguments
A<std::string>::A<std::string>(const std::string& s) {              
                ^~~~~~~~~~~~~~
1 error generated.

I just wanted to confirm if anyone knew if clang was correct in disallowing this or if it's a bug in clang. If I remove the explicit template argument, then clang works just fine with deducing the template argument, compiles and produces the correct output. It's only with the explicit template argument that it fails to compile, and I can't think of anything in the standard that would disallow that.

Thanks

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.417469 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO