我想从char *和“kernel32.dll”制作整数常量,但总是失败。以下是我失败的尝试,有人可以告诉我正确的用法吗?
error 1: cout << std::integral_constant<const char*, "kernel32.dll">::value << endl;
error 2: cout << std::integral_constant<char*, "kernel32.dll">::value << endl;
error 3: cout << std::integral_constant<char[], "kernel32.dll">::value << endl;
error 4: cout << cout << std::integral_constant<char*, static_cast<char*>("kernel32.dll")>::value << endl;
以上4个语句具有相同的错误信息。:
Console.cpp(181): error C2762: 'std::integral_constant' : invalid expression as a template argument for '_Val'
1> D:\Programfiles\Visual Studio 2013\VC\include\xtr1common(35) : see declaration of 'std::integral_constant'
1>Console.cpp(181): error C2955: 'std::integral_constant' : use of class template requires template argument list
1> D:\Programfiles\Visual Studio 2013\VC\include\xtr1common(35) : see declaration of 'std::integral_constant'
1>Console.cpp(181): warning C4552: '<<' : operator has no effect; expected operator with side-effect
更新
std::integral_constant<std::string, "abc">::value
也不会编译。
结束更新
这是我的场景,我做了一个简单的演示来演示我的目的:
#include <iostream>
#include <type_traits>
template< typename R, typename C, typename... Args>
class delegate
{
public:
template<R(C::*F)(Args...), typename ... Ts>
struct adapter
{
static R invoke_no_fwd(Args... args)
{
C t((Ts::value)...);
return (t.*F)(args...);
}
};
};
class Class
{
public:
Class(const char* psz) {
std::cout << psz << std::endl;
}
void print(int v)
{
std::cout << "Class: " << v << std::endl;
}
};
int main()
{
typedef void(*function_t)(int);
function_t ptrFunc = delegate<void, Class, int>::adapter<&Class::print, std::integral_constant<char*, "money"> >::invoke_no_fwd;
auto type = delegate<void, Class, int>::adapter<&Class::print, std::integral_constant<int, 42>>::invoke_no_fwd;
ptrFunc(-42); // 0
type(0); // 42
return 0;
}
答案 0 :(得分:2)
代码中的模板参数类型(目前使用std::integral_constant<>
实例化)仅用于访问::value
静态成员,因此您可以将其替换为定义value
成员的任何其他类型,如下图所示:
#include <iostream>
template <typename T>
void print()
{
std::cout << (T::value) << std::endl;
}
struct X
{
static const char* value;
};
const char* X::value = "ABC";
int main()
{
print<X>();
}
也就是说,只需将X
代替std::integral_constant<>
。
function_t ptrFunc
= delegate<void, Class, int>
::adapter<&Class::print, X /*here!*/>
::invoke_no_fwd;
更新1
如果你想在模板实例化中指定字符串的内联内容,下面的代码就可以了:
template <char... Chars>
struct MyString
{
static constexpr char value[] = { Chars..., '\0' };
};
template <char... Chars>
constexpr char MyString<Chars...>::value[];
// MyString<'A', 'B', 'C'>::value is same as const char[4] = { "ABC" };
function_t ptrFunc
= delegate<void, Class, int>
::adapter<&Class::print, MyString<'A', 'B', 'C'>>
::invoke_no_fwd;
更新2
如果您厌倦了输入MyString<'k','e','r','n','e','l','3','2','.','d','l','l'>
,则可以使用下面的宏(为简单起见,将"kernel32.dll"
等原始字符串扩展为符合MyString<char...>
模板的逗号分隔字符列表限于32个字符长的字符串):
#include <iostream>
#define STR_1(S,I) (I < sizeof(S) ? S[I] : '\0')
#define STR_2(S,I) STR_1(S,I), STR_1(S,I+1)
#define STR_4(S,I) STR_2(S,I), STR_2(S,I+2)
#define STR_8(S,I) STR_4(S,I), STR_4(S,I+4)
#define STR_16(S,I) STR_8(S,I), STR_8(S,I+8)
#define STR_32(S,I) STR_16(S,I), STR_16(S,I+16)
#define STR(S) STR_32(S,0)
template <char... Chars>
struct MyString
{
static constexpr char value[] = { Chars..., '\0' };
};
template <char... Chars>
constexpr char MyString<Chars...>::value[];
int main()
{
std::cout << MyString<STR("kernel32.dll")>::value << std::endl;
}
答案 1 :(得分:1)
您可以声明static const char*
并在std::integral_constant
中使用它,例如:
static constexpr const char money[] = "money";
int main()
{
typedef void(*function_t)(int);
function_t ptrFunc =
delegate<void, Class, int>
::adapter<
&Class::print,
std::integral_constant<const char*, money> >::invoke_no_fwd;
auto type = delegate<void, Class, int>::
adapter<&Class::print, std::integral_constant<const char*, money>>::invoke_no_fwd;
ptrFunc(-42); // 0
type(0); // 42
return 0;
}
您可以使用Aligning static string literals中的内容来允许使用宏在std::integral_constant
中写入文字字符串。