[英]Static private function with templated type as a default argument in C++
[英]Templated C++ function with the first argument set to the second as default argument
我有這樣的功能:
template <typename A, typename B>
void foo(const B & b)
{
...
}
A
應該是可選的; 如果未在函數調用中明確定義,則應將其設置為B
目的是避免不必要的冗長代碼:
int i;
// First variant: A is specified explicitly
foo<float>(i);
// Second variant: A is set to B implicitly
// This is because foo < int > (i) is unnecessarily verbose
foo(i);
但是,我還沒有辦法做到這一點。 任何人都能想出一個嗎?
#include <type_traits>
struct deduce_tag;
template <typename PreA = deduce_tag, typename B>
void foo(const B & b) {
using A = std::conditional_t<
std::is_same<PreA, deduce_tag>::value,
B,
PreA
>;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.