#include <iostream> namespace A { template<typename T, bool good> void foo1(T& value) { std::cout << "template<typename T, bool good> void foo1(T& value)" << std::endl; } template<typename T> void foo1(T& value) { std::cout << "template<typename T> void foo1(T& value)" << std::endl; } template<typename T, bool good, typename Predicate> void foo2(T& value, Predicate predicate = A::foo1<T, good>) { ((*predicate)(value)); } template<typename T, bool good, typename Predicate> void foo3(T& value, Predicate predicate = foo1<T, good>) { ((*predicate)(value)); } }; int main() { int n = 0; std::cout << "foo2>>>"; A::foo2<int, false, void (*)(int&)>(n); std::cout << "foo3>>>"; A::foo3<int, false, void (*)(int&)>(n); return 0; }
注释
template<typename T, bool good> void foo1(T& value)
或
template<typename T> void foo1(T& value)
编译时将弹出C2440错误:
error C2440: 'default argument' : cannot convert from 'void (__cdecl *)(T &)' to 'void (__cdecl *)(int &)'
None of the functions with this name in scope match the target type
运行编译环境:
Windows 7 Enterprise
Visual Studio 2008 SP1