Function template bizzare problem
// UsingDirective.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
#pragma once
#include <type_traits>
#pragma region CheckForFailureSignalPolicy
template<class Exception>
struct ThrowOnFailure;
template<class Exception>
struct NullOnFailure;
template<template<class>class FailureSignalPolicy>
struct IsThrowOnFailure;
template<>
struct IsThrowOnFailure<ThrowOnFailure>
{
enum {value = 1};
};
template<>
struct IsThrowOnFailure<NullOnFailure>
{
enum {value = 0};
};
#pragma endregion
template<int v>
struct Int2Type
{};
template<template<class> class FailurePolicy,class ExceptionType >
struct IReportFailure
{
enum {isThrowing = IsThrowOnFailure<FailurePolicy>::value};
#pragma region empty
static auto reportFailure()-> typename std::conditional<isThrowing,void,std::nullptr_t>::type
{
return rprt_help(Int2Type<isThrowing>());
}
static void rprt_help(Int2Type<true>)
{
throw ExceptionType();
}
static std::nullptr_t rprt_help(Int2Type<false>)
{
return nullptr;
}
#pragma endregion
//******************************************
template<class Argument>
static auto reportFailure(const Argument& arg)-> typename std::conditional<isThrowing,void,std::nullptr_t>::type
开发者_JAVA百科 {
return rprt_help(const Argument& arg,Int2Type<isThrowing>());
}
template<class Argument>
static void rprt_help(const Argument& arg,Int2Type<true>)
{
throw ExceptionType(arg);
}
template<class Argument>
static std::nullptr_t rprt_help(const Argument& arg,Int2Type<false>)
{
return nullptr;
}
#pragma region ExceptionType
static auto reportFailure(const ExceptionType& ex)-> typename std::conditional<isThrowing,void,std::nullptr_t>::type
{
return rprt_help(const ExceptionType& ex,Int2Type<isThrowing>());
}
static void rprt_help(const ExceptionType& ex,Int2Type<true>)
{
throw ExceptionType(ex);
}
static std::nullptr_t rprt_help(const ExceptionType& ex,Int2Type<false>)
{
return nullptr;
}
#pragma endregion
};
template<template<class> class FailurePolicy, class ExceptionType>
struct FailureSignalPolicy
{
enum {isThrowing = IsThrowOnFailure<FailurePolicy>::value};
static auto
signalFailure() -> typename std::conditional<IsThrowOnFailure<FailurePolicy>::value,void,std::nullptr_t>::type
{
return IReportFailure<FailurePolicy,ExceptionType>::reportFailure();
}
static auto
signalFailure(const ExceptionType& ex) -> typename std::conditional<IsThrowOnFailure<FailurePolicy>::value,void,std::nullptr_t>::type
{
return IReportFailure<FailurePolicy,ExceptionType>::reportFailure(ex);
}
///*******************************************
template<class Argument>
static auto
signalFailure(const Argument& arg) -> typename std::conditional<IsThrowOnFailure<FailurePolicy>::value,void,std::nullptr_t>::type
{
return IReportFailure<FailurePolicy,ExceptionType>::reportFailure(arg);
}
};
template<class ExceptionType>
struct ThrowOnFailure : private FailureSignalPolicy<ThrowOnFailure,ExceptionType>
{
using FailureSignalPolicy< ::ThrowOnFailure,ExceptionType>::signalFailure;
};
template<class ExceptionType>
struct NullOnFailure : private FailureSignalPolicy<NullOnFailure,ExceptionType>
{
using FailureSignalPolicy< ::NullOnFailure,ExceptionType>::signalFailure;
};
int _tmain(int argc, _TCHAR* argv[])
{
try
{
/*ThrowOnFailure<int>::signalFailure();
NullOnFailure<int>::signalFailure();*/
ThrowOnFailure<std::out_of_range>::signalFailure(1);
}
catch(...)
{
}
return 0;
}
Trying to compile this I'm getting bizzare errors:
Error 1 error C2143: syntax error : missing ')' before 'const' Error 2 error C2661: 'IReportFailure::rprt_help' : no overloaded function takes 0 arguments Error 3 error C2059: syntax error : ')'The problematic fnc is marked in code with ****
The body of IReportFailure::reportFailure
should be changed to :
return rprt_help(arg, Int2Type<isThrowing>());
精彩评论