Suppose I have some class:
template <typename T>
class Foo {
const T* x_;
public:
Foo(const T* str) : x_{str} {}
};
and I provide some user-defined literals that create a Foo
object:
Foo<char> operator"" _foo(const char* str, std::size_t) {
return Foo<char>{str};
}
Foo<wchar_t> operator"" _foo(const wchar_t* str, std::size_t) {
return Foo<wchar_t>{str};
}
// etc. for char16_t and char32_t.
My question is this: why can I not template these and save having to rewrite code?
template <typename T>
Foo<T> operator"" _foo(const T* str, std::size_t) {
return Foo<T>{str};
}
gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.4) and 7.0.0 (compiled myself) report:
error: ‘Foo<T> operator""_foo(const T*, std::size_t)’ has invalid argument list
Foo<T> operator"" _foo(const T* str, std::size_t) {
^
The error message seems to be clear enough, but I don't see a reason why I shouldn't be allowed to do this in principle; so, am I doing this incorrectly, or is this genuinely not allowed?