그래서 나는 그들이 printf
에 전달되기 전에 형식 인수를 필터링 할 "소독제"기능을 구축을 위해 노력하고 있습니다. 제대로 포맷하기 위해 템플릿 전문성과 완벽한 전달
template<typename A>
_CR_INLINE decltype(auto) sanitize_forward(A&& arg) {
return std::forward<A>(arg);
}
template<>
_CR_INLINE decltype(auto) sanitize_forward<std::string>(std::string&& arg) {
return std::forward<const char*>(arg.c_str());
}
은 그래서 모든
std::string
이하는
const char*
에 "붕괴"로되어있다.
template<typename...Args>
_CR_INLINE void printf_safe(const std::string& format, Args&&...args) {
std::printf(format.c_str(), sanitize_forward<Args>(args)...);
}
나는 인수가 내가 std::forward
을 반환 왜 완벽 printf
- 전달 ,되고 싶어요. 그러나 이것이 구현되어야하는 방법에 대해 정말로 머리를 감쌀 수는 없습니다.
1) decltype(auto)
맞습니까? std::forward
의 반환에 대한 r 값 참조를 유지해야합니다. 맞습니까? std::string&&
또는 std::string
:
2) 어떻게 템플릿을 전문으로해야합니까?
3) 추론 해 전달 참조 오른쪽 실제 형태와 동일해야?
당신은'printf'를 위해 앞으로 갈 필요가 없습니다. –
하지만 printf가 내부 로깅 기능이라고 가정 해 봅시다. 어떻게 구현해야합니까? 또한 왜 필요하지 않은 인수를 전달하는 것이 정확히 무엇입니까? – mutex36