Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 字符到类型的非constexpr转换_C++_Templates_Traits - Fatal编程技术网

C++ 字符到类型的非constexpr转换

C++ 字符到类型的非constexpr转换,c++,templates,traits,C++,Templates,Traits,我想知道在C++ 假设我有一个模板 template<typename T> struct Foo { static void foo(); }; 有没有更优雅的方法?我在考虑写一个chartrait,像这样: template<char c> struct char_trait {}; template<> struct char_trait<'i'> { using type = int; }; 模板 结构字符{}; 模

我想知道在
C++

假设我有一个模板

template<typename T>
struct Foo { 
    static void foo();
};
有没有更优雅的方法?我在考虑写一个
chartrait
,像这样:

template<char c>
struct char_trait {};

template<>
struct char_trait<'i'> {
    using type = int;
};
模板
结构字符{};
模板
结构特征{
使用type=int;
};
但是由于
c
是非constexpr,所以这没有多大意义

如果模板(traits)版本中提到的一些提示不适用于运行时评估,我将不胜感激

要避免长时间的
if(){}else if()
级联,您可以做的是在映射中组织您的内容:

std::map<char,std::function<void ()>> foo_calls {
    { 'i', Foo<int>::foo } ,
    { 'f', Foo<float>::foo } ,
    // ...
};
正如模板(traits)中提到的,该版本不适用于运行时评估

要避免长时间的
if(){}else if()
级联,您可以做的是在映射中组织您的内容:

std::map<char,std::function<void ()>> foo_calls {
    { 'i', Foo<int>::foo } ,
    { 'f', Foo<float>::foo } ,
    // ...
};

这是不可能的。C++中的所有模板实例化都是在编译时完成的。这是好是坏。无法在运行时实例化模板

尽管将变量的值转换为适当的函数调用有很多不同的方法。例如,您可以尝试:

switch (c)
{
    case 'i':
       Foo<int>::foo(); break;
    case 'f':
       Foo<float>::foo(); break;
}
开关(c)
{
案例“i”:
Foo::Foo();break;
案例“f”:
Foo::Foo();break;
}
这为编译器优化此选择提供了更多空间
这是不可能的。C++中的所有模板实例化都是在编译时完成的。这是好是坏。无法在运行时实例化模板

尽管将变量的值转换为适当的函数调用有很多不同的方法。例如,您可以尝试:

switch (c)
{
    case 'i':
       Foo<int>::foo(); break;
    case 'f':
       Foo<float>::foo(); break;
}
开关(c)
{
案例“i”:
Foo::Foo();break;
案例“f”:
Foo::Foo();break;
}
这为编译器优化此选择提供了更多空间
..

如果
c
是运行时变量,则不能在templateDefine中使用它。你的第一个解决方案有什么不雅之处?如果
c
是运行时变量,则不能在templateDefine中使用它。你的第一个解决方案有什么不雅之处?第二种方法,即使有效,是如何改进的?