您可以將接口與實現分離,使用重載和enable_if: namespace detail{ struct Empty {}; template<typename T> void FooImpl(int x,int y, int z, T w) { constexpr test = std::is_same_v<T, Empty>; if constexpr(test) { //use w for something } }}template<bool test>std::enable_if_t<test> void Foo(int x, int y, int z, int w){ detail::FooImpl(x, y, z, w);}template<bool test>std::enable_if_t<!test> void Foo(int x, int y, int z){ detail::FooImpl(x, y, z, detail::Empty{});} 注意,在泛型情況下,您可能會在接口上使用std::forward<>,并且可能(使用static_assert())記錄w的類型是Empty或int。