我怎样(在编译时)确定typename是否是函数指针typename?

Bil*_*eal 3 c++ template-meta-programming

考虑Win32的Runtime Dynamic Linking机制的以下包装器:

#include <boost/noncopyable.hpp>
#include <windows.h>
#include "Exception.hpp"

namespace WindowsApi
{
    class RuntimeDynamicLinker : boost::noncopyable
    {
        HMODULE hMod_;
    public:
        RuntimeDynamicLinker(const wchar_t * moduleName)
        {
            hMod_ = LoadLibraryW(moduleName);
            if (hMod_ == 0)
            {
                Exception::Throw(GetLastError());
            }
        }
        template <typename T>
        T GetFunction(const char* functionName)
        {
            FARPROC result = GetProcAddress(hMod_, functionName);
            if (result == 0)
            {
                Exception::Throw(GetLastError());
            }
            return reinterpret_cast<T>(result);
        }
        ~RuntimeDynamicLinker()
        {
            FreeLibrary(hMod_);
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

一个示例客户端:

typedef NTSTATUS (NTAPI * NtQueryInformationProcess_t)(
    IN HANDLE,
    IN PROCESS_INFORMATION_CLASS,
    OUT PVOID,
    IN ULONG,
    OUT PULONG);
RuntimeDynamicLinker ntdll(L"ntdll.dll");
NtQueryInformationProcess_t NtQueryInformationProcess = 
    ntdll.GetFunction<NtQueryInformationProcess_t>("NtQueryInformationProcess");
Run Code Online (Sandbox Code Playgroud)

基本上,我想添加一条错误消息,如果有人试图使用除了函数指针类型之外的任何GetFunction地方T(因为reinterpret_cast我不得不在这里使用,否则可能会隐藏用户错误).

通过提升类型特征挖掘,我确实发现现有is_function模板.但是,is_function接受对函数的引用,在我的情况下这将是用户错误(仅限函数指针).

RuntimeDynamicLinker::GetFunction<T>()如果T不是函数指针类型,我如何修改以产生合理可理解的编译器错误消息?

(旁注:我从未做过任何类型的TMP,所以不要害怕过去对TMP的常规用户来说是"基本"的东西)

Joe*_*oeG 5

你可以is_pointer<T>::value && is_function<remove_pointer<T>::type>::valuestatic_assert/中使用BOOST_STATIC_ASSERT.