std::integral_constant<ch​​ar*, "kernel32.dll"> 无法编译?

Tri*_*ant 2 c++ templates c++11 visual-studio-2012

我想从 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;
Run Code Online (Sandbox Code Playgroud)

以上 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
Run Code Online (Sandbox Code Playgroud)

更新:

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;
}
Run Code Online (Sandbox Code Playgroud)

Pio*_*cki 5

您代码中的模板参数类型,当前使用 实例化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>();
}
Run Code Online (Sandbox Code Playgroud)

也就是说,只要把X到位的std::integral_constant<>。

function_t ptrFunc
    = delegate<void, Class, int>
          ::adapter<&Class::print, X /*here!*/>
          ::invoke_no_fwd;
Run Code Online (Sandbox Code Playgroud)

现场演示链接。


更新 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;
Run Code Online (Sandbox Code Playgroud)

另一个现场演示链接。


更新 2

如果您厌倦了输入,MyString<'k','e','r','n','e','l','3','2','.','d','l','l'>您可以使用以下宏(为简单起见仅限于 32 个字符长的字符串)将原始字符串扩展为"kernel32.dll"符合MyString<char...>模板的逗号分隔字符列表:

#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;
}
Run Code Online (Sandbox Code Playgroud)

另一个现场演示链接

  • @Triumphant:请稍等,但您正在使用 `std::integral_constant`,您认为它是什么?它是一个“静态”值 (2认同)