包装微软" W"和" A"功能

时间:2016-08-17 15:53:02

标签: c++ c++11 c++14

我正在编写一个使用臭名昭着的Microsoft var chai = require('chai'); var chaiAsPromised = require('chai-as-promised'); chai.use(chaiAsPromised); var expect = chai.expect; module.exports = function () { this.Given(/^I launch the protractor demo page$/, function () { return browser.get('http://juliemr.github.io/protractor-demo/'); }); }); this.When(/^I check the title of the page$/, function () { return browser.getTitle().then(function(text){ console.log('title is - ' + text); expect(text).to.equal('Super Calculator'); }); }); A函数的模板函数。模板参数类型仅限于Wstd::string,因此我使用std::wstring包装器来调用所需的函数:

struct

然而,这是一个痛苦...有没有更好的方法来实现这一目标?我尝试使用函数模板,如下所示:

template<typename type>
struct formatMessageT;

template<>
struct formatMessageT<std::string> {
    static inline size_t call(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPSTR lpBuffer, DWORD nSize, va_list *Arguments) {
        return FormatMessageA(dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments);
    }
};

template<>
struct formatMessageT<std::wstring> {
    static inline size_t call(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPWSTR lpBuffer, DWORD nSize, va_list *Arguments) {
        return FormatMessageW(dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments);
    }
};

不幸的是,这仅在函数参数类型匹配时才有效(假设参数本身也匹配):

#include <iostream>

char funW(wchar_t) {
    return 'W';
}

char funA(char) {
    return 'A';
}

template<typename returnType, typename function, typename ...Args>
returnType functionCall(function f, Args ...args) {
    return f(args...);
}

int main() {
    bool dummy = true;
    std::cout << functionCall<char>(dummy ? funW : funA, dummy ? L'W' : 'A') << std::endl;

    std::cin.get();

    return 0;
}

char funW(char) {
    return 'W';
}

char funA(char) {
    return 'A';
}

1 个答案:

答案 0 :(得分:0)

一个删除了他的答案的人发布的解决方案对微软的编译器不起作用,但是在修改代码后我设法调整它并使其工作。非常感谢你(W.F或类似的人)!

{{1}}