传递“const char"的一些麻烦模板中的参数,即使在 Visual C++ Compiler Nov 2013 CTP [英] Some trouble passing a "const char" parameter in templates even in Visual C++ Compiler Nov 2013 CTP

查看:57
本文介绍了传递“const char"的一些麻烦模板中的参数,即使在 Visual C++ Compiler Nov 2013 CTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,新年快乐,

即使在 Visual C++ 的最新版本 Compiler Nov 2013 CTP 中,我在传递模板中的const char"参数时也遇到了一些麻烦!这是在最新的 Visual C++ 编译器中不起作用的简单代码,但在带有std=c++x0"选项的g++"中是可行的,

I have some troubles passing passing a "const char" parameter in templates even in the last versión Compiler Nov 2013 CTP of Visual C++! This is the simple code that doesn't work in the latest Visual C++ compiler but yes with "g++" with the option "std=c++x0",

#include <stdlib.h>


template<char _parameterChar>
class A
{
    char varChar;
public:
    A(){ varChar = _parameterChar; }
    ~A(){}
};


int main(int argc, char* argv[])
{
    const char a_1 = 'a';
    const char a_2 = "abcdef"[0]; // This instruction gets a constant 'a'.


    A<'a'> first_A; // compile ok!
    A<a_1> second_A; // compile ok!
    A<a_2> third_A; // --->  This not compiles!! Why not ?!?!?!

    return 0;
}

Visual C++ 编译器给出了那个错误,

Visual C++ compiler gives that error,

error C2971: 'A' : template parameter '_parameterChar' : 'a_2' : a local variable cannot be used as a non-type argument

我认为这是编译器的限制,因为abcdef"[0],你可以在编译时得到 const char 'a',不是吗?

I think that is a restricción of compiler, because "abcdef"[0], you could get the const char 'a' at compile time, isn't it ?

推荐答案

目前,此代码在 Visual Studio 2017 (Windows) 上按预期编译和工作.无需进行特定的配置设置,只需默认控制台应用程序即可.不幸的是,我无法在以前版本的 VS 上检查它.

Currently this code compiles and works as expected on Visual Studio 2017 (Windows). No need to do a specific configuration settings, just enough default console application. Unfortunately I haven't ability to check it on previous versions of VS.

这篇关于传递“const char"的一些麻烦模板中的参数,即使在 Visual C++ Compiler Nov 2013 CTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆