不会编译?-6ren"> 不会编译?-我想从 char* 和“kernel32.dll”中获取整数常量,但总是失败。以下是我失败的尝试,谁能告诉我正确的用法? error 1: cout ::value ::value ::value (-6ren">
gpt4 book ai didi

c++ - std::integral_constant 不会编译?

转载 作者:行者123 更新时间:2023-11-28 00:24:51 25 4
gpt4 key购买 nike

我想从 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;
}

最佳答案

代码中的模板参数类型,当前使用 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;

Live demo link.


更新 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;

Another live demo link.


更新 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;
}

Yet another live demo link

关于c++ - std::integral_constant<ch​​ar*, "kernel32.dll"> 不会编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25580294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com