我想實現一個模板化函數,以便函數只接受某些特定類型的輸入。我試過std :: enable_if和std :: is_base_of來實現目標。簡化代碼如下所示。代碼編譯並在啓用C++ 11時使用clang ++或g ++運行。但是,當我用Visual Studio 2013進行編譯時,它總是說could not deduce template argument for 'is_base_class'
。誰能告訴我爲什麼Visual Studio 2013不能編譯以及如何解決這個問題?爲什麼模板化的C++函數不能在Visual Studio 2013下編譯
#include <type_traits>
#include <map>
#include <unordered_map>
#include <iostream>
using namespace std;
template <class Type>
struct TypeWrapper {};
template <class T0, class T1>
struct CompoundType : public TypeWrapper<T0>, public TypeWrapper<T1> {};
template <class Type,
typename std::enable_if<
std::is_base_of<
TypeWrapper<Type>,
CompoundType<
std::map<typename Type::key_type, typename Type::mapped_type>,
std::unordered_map<typename Type::key_type, typename Type::mapped_type>
>
>::value
, Type
>::type* is_base_class = nullptr
>
void TemplatedFunction(Type map) {
std::cout << "Hello, world!" << std::endl;
}
int main(int argc, char *argv[])
{
std::unordered_map<int, double> a;
TemplatedFunction(a);
}
你能減少示例代碼任何進一步的?另外,如果你希望人們(包括你自己)琢磨它的結構,請一致縮進。 –
FYI - VS2015正常工作。 – Blacktempel
VS 2013中的SFINAE支持有問題。除了遷移到2015年,您可能沒有任何工作。 – bpeikes