首页 > 解决方案 > 当且仅当它不会导致重新定义错误时,如何定义新的函数/结构/类?

问题描述

我正在生成 c++ 代码,当且仅当它不会导致重新定义错误时,我才需要定义一个新的函数/结构/类。我怎样才能做到这一点?我认为解决方案是尝试重新定义新的函数/结构/类并使其以 SFINAE 友好的方式失败,但我不知道如何做到这一点。

例如:

int foo (int x, int y) { 
   return x + y;
}

int foo (int z, int a) { // Obvious redefinition here, how can I make it fail in a SFINAE friendly way?
   return x * y;
} 

template <typename T, typename U>
struct bar {};

template <typename T, typename U> // Partial template specialisation does not actually specialise the parent template, how do I make this definition fail in a SFINAE friendly way?
struct bar<T, U> {}; 

标签: c++c++20sfinaeredefinition

解决方案


推荐阅读