如果使用默认模板,如何产生编译器错误?

How to produce compiler error if default template is used?

使用模板特化,我编写了一系列具有相同名称和相同参数类型的函数,但返回模板参数指定类型的数据:

template<typename T> T     f          (int x); // Purposefully unimplemented.

template<> inline uint8_t  f<uint8_t> (int x) { return f8  [x]; }
template<> inline uint16_t f<uint16_t>(int x) { return f16 [x]; }
template<> inline uint32_t f<uint32_t>(int x) { return f32 [x]; }
template<> inline uint64_t f<uint64_t>(int x) { return f64 [x]; }

然后我可以这样写代码:

uint32_t a = f<uint32_t>(3);
uint64_t b = f<uint64_t>(7);

如果有人试图将 f 的版本用于我为其定义的专用类型之外的任何其他内容,我故意未实现默认模板以产生链接器错误。

我有两个问题:

1) 如果有人试图使用比我得到的更友好的默认模板,我有什么方法可以使用 static_assert()(或其他)来产生编译错误(而不是链接器错误)现在:对 `int f(int)' 的未定义引用?

2) 有没有什么方法可以使用模板来做到这一点,该模板对程序员保持相同的接口,但不需要模板专门化? (即,是否有某种方法可以完全避免使用默认模板?)

namespace fimpl{
  template<class T>struct tag_t{};
  template<class T>
  void fimpl(tag_t<T>, int x)=delete;
}

template<typename T> T     f          (int x){ using fimpl::fimpl; return fimpl(fimpl::tag_t<T>{}, x); }

现在不专精;覆盖。

namespace fimpl{ inline uint8_t  fimpl(tag_t<uint8_t>,  int x) { return f8  [x]; } }
namespace fimpl{ inline uint16_t fimpl(tag_t<uint16_t>, int x) { return f16 [x]; } }
namespace fimpl{ inline uint32_t fimpl(tag_t<uint32_t>, int x) { return f32 [x]; } }
namespace fimpl{ inline uint64_t fimpl(tag_t<uint64_t>, int x) { return f64 [x]; } }

这使用标签分派来选择覆盖而不是使用专门化。

如果未找到显式特化,则会选择 =delete 模板,您会立即收到编译器错误。

有趣的是,如果你想用新类型扩展它,比如 namespace lib{ struct bigint; } 你可以在 namespace lib 中放置一个 fimpl(fimpl::tag_t<bigint>, int) 重载,它会起作用。我怀疑你是否需要它。

如果您同意 f(tag<uint8_t>, 7) 而不是 f<uint8_t>(7),您也可以取消 f 作为模板。只需删除 fimpl 命名空间(从中移出内容),将 fimpl::fimpl 重命名为 f,删除 =deleteed 模板函数,添加 template<class T> constexpr tag_t<T> tag{};。但是语法在调用点有点不同。

1) Is there some way I can use static_assert (or whatever) to produce a compile error (instead of a linker error) if someone tries to use the default template that's more friendly than what I get now: undefined reference to `int f(int)'?

我认为更好的解决方案是路人在评论中建议的解决方案:

template<typename T> T f (int x) = delete;

但是如果你真的想使用static_assert()...我想你可以尝试如下

template<typename T>
T f (int x)
 {
   static_assert( sizeof(T) == std::size_t(-1), "f()!" );

   return {};
 }

2) Is there some way to do this with templates that maintains the same interface to the programmer, but doesn't require template specialization? (I.e., is there some way to avoid a default template altogether?)

我不清楚到底想要什么。

您不想要专业化并且想要避免默认模板?

假设您只需要仅适用于一组特定类型的默认模板,我想您可以使用 SFINAE。

举个例子,下面的f()只有在T是整数类型时才会启用

template<typename T>
typename std::enable_if<std::is_integral<T>{}, T>::type f (int x)
 { return x; }

下面是一个完整的编译示例

#include <iostream>
#include <type_traits>

template<typename T>
typename std::enable_if<std::is_integral<T>{}, T>::type f (int x)
 { return x; }

int main ()
 {
   auto f16 = f<std::uint16_t>(0);
   auto f32 = f<std::uint32_t>(0);

   static_assert( std::is_same<decltype(f16), std::uint16_t>{}, "!" );
   static_assert( std::is_same<decltype(f32), std::uint32_t>{}, "!" );

   // compilation error
   // auto fd = f<double>(0);
 }