Std:Enable_if可以有条件地编译成员函数
std::enable_ifstd::enable_if
#include <utility>#include <iostream>template< class T >class Y {
public:
template < typename = typename std::enable_if< true >::type >
T foo() {
return 10;
}
template < typename = typename std::enable_if< false >::type >
T foo() {
return 10;
}};int main() {
Y< double > y;
std::cout << y.foo() << std::endl;}% LANG=C make CXXFLAGS="-std=c++0x" enable_if g++ -std=c++0x enable_if.cpp -o enable_if enable_if.cpp:12:65: error: `type' in `struct std::enable_if<false>' does not name a type enable_if.cpp:13:15: error: `template<class T> template<class> T Y::foo()' cannot be overloaded enable_if.cpp:9:15: error: with `template<class T> template<class> T Y::foo()'
std::enable_if< bool, T = void >::type
LEATH
哔哔one
DIEA
慕仙森
随时随地看视频慕课网APP
相关分类