template enable_if 功能实现是否可行?
Is template enable_if function implementation possible?
使用 c++14,我有一些类似于以下的函数声明。
template <class... Args>
struct potato {
template <class T, class = std::enable_if_t<!std::is_same<T, int>::value>>
const T& blee(size_t blou) const;
template <class T, class = std::enable_if_t<std::is_same<T, int>::value>>
const T& blee(size_t blou) const;
};
是否可以单独实现功能?据我所知,编译器无法弄清楚什么在实现什么。例如:
template <class... Args>
template <class T, class>
const T& potato<Args...>::blee(size_t blou) const {
// do something
}
template <class... Args>
template <class T, class>
const T& potato<Args...>::blee(size_t blou) const {
// do something
}
enable_if
信息在此时丢失。我是否在我的工具包中遗漏了一个技巧来完成这项工作?请注意,我宁愿不使用 return 类型 enable_if
或参数 enable_if
,因为它们是不敬虔的。
编辑:已更新以更好地代表我的用例。
你真的不需要 enable_if
:
template<class T>
const T& blee(size_t blou) const {
// do something
}
template<>
const int& blee<int>(size_t blou) const {
// do something
}
编辑:由于您的函数在 class 模板中,您将不得不使用标签调度:
template<class... Args>
struct potato {
template<class T>
void blee() const;
private:
void realBlee(std::true_type) const;
void realBlee(std::false_type) const;
};
template<class... Args>
template<class T>
void potato<Args...>::blee() const {
realBlee(std::is_same<T, int>());
}
template<class... Args>
void potato<Args...>::realBlee(std::true_type) const {
std::cout << "int\n";
}
template<class... Args>
void potato<Args...>::realBlee(std::false_type) const {
std::cout << "generic\n";
}
或类似的东西,如 constexpr if:
template<class... Args>
struct potato {
template<class T>
void blee() const;
private:
void intBlee() const;
};
template<class... Args>
template<class T>
void potato<Args...>::blee() const {
if constexpr (std::is_same_v<T, int>) {
intBlee();
} else {
std::cout << "generic\n";
}
}
template<class... Args>
void potato<Args...>::intBlee() const {
std::cout << "int\n";
}
The enable_if information is lost at that point.
没有丢,两种情况都是int
。只有一个模板没有被实例化。
使用 c++14,我有一些类似于以下的函数声明。
template <class... Args>
struct potato {
template <class T, class = std::enable_if_t<!std::is_same<T, int>::value>>
const T& blee(size_t blou) const;
template <class T, class = std::enable_if_t<std::is_same<T, int>::value>>
const T& blee(size_t blou) const;
};
是否可以单独实现功能?据我所知,编译器无法弄清楚什么在实现什么。例如:
template <class... Args>
template <class T, class>
const T& potato<Args...>::blee(size_t blou) const {
// do something
}
template <class... Args>
template <class T, class>
const T& potato<Args...>::blee(size_t blou) const {
// do something
}
enable_if
信息在此时丢失。我是否在我的工具包中遗漏了一个技巧来完成这项工作?请注意,我宁愿不使用 return 类型 enable_if
或参数 enable_if
,因为它们是不敬虔的。
编辑:已更新以更好地代表我的用例。
你真的不需要 enable_if
:
template<class T>
const T& blee(size_t blou) const {
// do something
}
template<>
const int& blee<int>(size_t blou) const {
// do something
}
编辑:由于您的函数在 class 模板中,您将不得不使用标签调度:
template<class... Args>
struct potato {
template<class T>
void blee() const;
private:
void realBlee(std::true_type) const;
void realBlee(std::false_type) const;
};
template<class... Args>
template<class T>
void potato<Args...>::blee() const {
realBlee(std::is_same<T, int>());
}
template<class... Args>
void potato<Args...>::realBlee(std::true_type) const {
std::cout << "int\n";
}
template<class... Args>
void potato<Args...>::realBlee(std::false_type) const {
std::cout << "generic\n";
}
或类似的东西,如 constexpr if:
template<class... Args>
struct potato {
template<class T>
void blee() const;
private:
void intBlee() const;
};
template<class... Args>
template<class T>
void potato<Args...>::blee() const {
if constexpr (std::is_same_v<T, int>) {
intBlee();
} else {
std::cout << "generic\n";
}
}
template<class... Args>
void potato<Args...>::intBlee() const {
std::cout << "int\n";
}
The enable_if information is lost at that point.
没有丢,两种情况都是int
。只有一个模板没有被实例化。