问题描述
我想要一个在其构造函数中接受两个参数的类。第一个可以是一个int,double或float,所以< typename T>
,第二个总是一个字符串字符串my string * const。
I want a class which takes two parameters in its constructor. The first can be either an int, double or float, so <typename T>
, and the second is always a string literal "my string", so I guess const char * const.
任何人都可以给我一些可编译的代码,声明一个简单的类模板并声明一个类的对象?
Can anyone give me some compilable code which declares a simple class template as described and declares an object of that class?
感谢
推荐答案
对不起,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" );
这篇关于将字符串文字作为参数传递给C ++模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!