Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
C++: Is there a way with templates to overload << to do a no-op on a certain class specialization?
My class N takes a type T and a varying amount of types F...
What's really happening is that I am overloading operator() to take a function reference and the arguments that that function will be supplied. It's like a bind function. But when the function reference returns void, I don't want to be able to std::cout the value of that function call from operator(). So I added an overload of operator<< for std::cout to NOT do anything but I guess I'm not writing out the function signature correctly because I get the same errors. Does anyone know what I'm doing wrong?
#include <iostream>
#include <utility>
template <typename T>
struct N;
template <typename T, typename ... F>
struct N<T(F...)> {
T operator()(T (&t)(F...), F&&... f) {
return t(std::forward<F>(f)...);
�� }
};
template <typename ... T> void operator<< (std::ostream &, const N<void(T...)> &) {}
void f(int, int) {}
int main() {
N<void(int, int)> bind;
std::cout << bind(f, 5, 4);
}
Here's a demo -- http://ideone.com/t5eK7d#view_edit_box
The above code fails because I'm printing out a function that returns void; and that is the function f. My overload of operator<< doesn't seem to be affecting anything. If you need any more detail just say so. Thanks.
1 Answer
- ?Lv 78 years agoFavorite Answer
In the expression std::cout << bind(f, 5, 4);, you're passing the *result* of invoking your bind() to operator<<, that is, you're passing void. It's the same as, for example, cout << sin(x)
To make it invoke your template, use std::cout << bind;