将字符串文字作为类型参数传递给类模板 [英] Passing a string literal as a type argument to a class template

查看:95
本文介绍了将字符串文字作为类型参数传递给类模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想声明一个类模板,其中的模板参数之一采用字符串文字,例如 my_class<字符串"> .

I want to declare a class template in which one of the template parameters takes a string literal, e.g. my_class<"string">.

有人可以给我一些可编译的代码,该代码声明所描述的简单类模板吗?

Can anyone give me some compilable code which declares a simple class template as described?

注意:该问题的先前的措辞在询问者实际上试图完成的工作上含糊不清,并且应该关闭得不够清楚.但是,此后此问题多次被称为规范的字符串文字类型参数"问题.因此,已重新措辞以同意该前提.

Note: The previous wording of this question was rather ambiguous as to what the asker was actually trying to accomplish, and should probably have been closed as insufficiently clear. However, since then this question became multiple times referred-to as the canonical ‘string literal type parameter’ question. As such, it has been re-worded to agree with that premise.

推荐答案

对不起,C ++当前不支持将字符串文字(或真实文字)用作模板参数.

Sorry, C++ does not currently support the use of string literals (or real literals) as template parameters.

但是重新阅读您的问题,那是您在问什么?你不能说:

But re-reading your question, is that what you are asking? You cannot say:

foo <"bar"> x;

但是你可以说

template <typename T>
struct foo {
   foo( T t ) {}
};

foo <const char *> f( "bar" );

这篇关于将字符串文字作为类型参数传递给类模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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