Can template declarations take string types as template parameters,
just like int types? If not, why it has been designed like that?
Ganesh 11 1572
Ganesh wrote: Can template declarations take string types as template parameters, just like int types? ...
Nope. No floating point types either.
...If not, why it has been designed like that?
Well, consider and example from C++ Templates: The Complete Guide
template <char const* msg>
class Foo { public: void print(); };
int main()
{
Foo<"Hiya!">().print();
}
Two instances of Foo are the same type if and only if they have the
same arguments. Here the argument is a pointer value - in other words
an address. Two identical string literals need not have the same address,
so Foo<"X"> and in some other source file say Foo<"X"> could be two
distinct and hence incompatible types.
Floating point non-type template arguments are less problematic.
Ganesh wrote: Can template declarations take string types as template parameters, just like int types? If not, why it has been designed like that?
Ganesh
They can be native types (int, char, long, double, etc) and
any class type (including other template classes), as well as
pointers to all of these. So yes, std::string is fine.
Larry
Larry I Smith wrote: Ganesh wrote:Can template declarations take string types as template parameters, just like int types? If not, why it has been designed like that?
Ganesh
They can be native types (int, char, long, double, etc) and any class type (including other template classes), as well as pointers to all of these. So yes, std::string is fine.
Larry
Opps, forget the pointers.
Larry
Michael Olea wrote: Ganesh wrote:
Can template declarations take string types as template parameters, just like int types? ...
Nope. No floating point types either.
...If not, why it has been designed like that?
Well, consider and example from C++ Templates: The Complete Guide
template <char const* msg> class Foo { public: void print(); };
int main() { Foo<"Hiya!">().print(); }
Two instances of Foo are the same type if and only if they have the same arguments. Here the argument is a pointer value - in other words an address. Two identical string literals need not have the same address, so Foo<"X"> and in some other source file say Foo<"X"> could be two distinct and hence incompatible types.
Floating point non-type template arguments are less problematic.
These templates all work just fine, both pointer and non-pointer
variants. So pointers are valid.
std::vector<int> iv;
std::vector<int *> ipv;
std::vector<char> cv;
std::vector<char *> cpv;
std::vector<double> dv;
std::vector<double *> dpv;
Larry
Larry I Smith wrote: Michael Olea wrote: Ganesh wrote:
Can template declarations take string types as template parameters, just like int types? ...
Nope. No floating point types either.
...If not, why it has been designed like that?
Well, consider and example from C++ Templates: The Complete Guide
template <char const* msg> class Foo { public: void print(); };
int main() { Foo<"Hiya!">().print(); }
Two instances of Foo are the same type if and only if they have the same arguments. Here the argument is a pointer value - in other words an address. Two identical string literals need not have the same address, so Foo<"X"> and in some other source file say Foo<"X"> could be two distinct and hence incompatible types.
Floating point non-type template arguments are less problematic.
These templates all work just fine, both pointer and non-pointer variants. So pointers are valid.
std::vector<int> iv; std::vector<int *> ipv; std::vector<char> cv; std::vector<char *> cpv; std::vector<double> dv; std::vector<double *> dpv;
Larry
I believe the OP was talking about *non-type* template parameters. For
example:
std::bitset<42> aSet;
where bitset is something like:
template<size_t N> class bitset { ... };
What is not legal (though some compilers have extensions allowing it) is
template <float x> class Foo { ... }; Foo<42.0> aFoo; or
template <const char* s> class Bar { ... }; Bar<"Hello, World!"> aBar;
Your examples above are all type parameters - not relevant to the question
of what non-type parameters are legal.
Michael Olea wrote: Larry I Smith wrote:
Michael Olea wrote:Ganesh wrote:
Can template declarations take string types as template parameters, just like int types? ... Nope. No floating point types either.
[snip]
These templates all work just fine, both pointer and non-pointer variants. So pointers are valid.
std::vector<int> iv; std::vector<int *> ipv; std::vector<char> cv; std::vector<char *> cpv; std::vector<double> dv; std::vector<double *> dpv;
Larry
I believe the OP was talking about *non-type* template parameters. For example:
[snip]
I didn't get that at all from the original question:
"Can template declarations take string types as template
parameters, just like int types?"
std::string is a valid template parameter, so my answer
to the quoted question would be 'yes'.
Oh well...
Larry
Larry I Smith wrote:
[...] I believe the OP was talking about *non-type* template parameters. For example:
[snip]
I didn't get that at all from the original question:
"Can template declarations take string types as template parameters, just like int types?"
Yeah - we were answering different questions. The question does contain the
word "types" so maybe you are right about what was being asked. Either way,
the question (whichever version it was) has been answered ;-)
"Michael Olea" <ol***@sbcglobal.net> wrote in message
news:HL******************@newssvr14.news.prodigy.c om... Ganesh wrote:
Can template declarations take string types as template parameters, just like int types? ... Nope. No floating point types either.
How do you figure that? Any type, primitive or not, can be substituted in a
template. One does not arm a template with a literal or instance of a type. ...If not, why it has been designed like that?
Well, consider and example from C++ Templates: The Complete Guide
template <char const* msg> class Foo { public: void print(); };
int main() { Foo<"Hiya!">().print(); }
#include <iostream>
#include <string>
template< class S >
class Foo
{
S m_s;
public:
Foo(S s) : m_s(s) { }
~Foo() { }
friend std::ostream& operator<<(std::ostream& os, Foo< S >& r_foo);
};
template< class S >
std::ostream& operator<<(std::ostream& os, Foo< S >& r_foo)
{
std::cout << "Foo's member is " << r_foo.m_s << "\n";
return os;
}
int main()
{
Foo<std::string> s_foo("a short string");
std::cout << s_foo;
Foo<double> d_foo(99.9);
std::cout << d_foo;
return 0;
}
/*
Foo's member is a short string
Foo's member is 99.9
*/
Peter Julian wrote: "Michael Olea" <ol***@sbcglobal.net> wrote in message news:HL******************@newssvr14.news.prodigy.c om... Ganesh wrote:
> Can template declarations take string types as template parameters, > just like int types? ... Nope. No floating point types either.
How do you figure that?
Simple - I took the question to be about non-type template parameters.
Any type, primitive or not, can be substituted in a template.
Right, but for non-type template parameters there are restrictions - no
floating point types, for example.
One does not arm a template with a literal or instance of a type.
You can find lots of examples of templates designed to be instantiated with
values (compile time constant experssions) in stl, boost, books by
Alexandrescu, Vandevoorde and Josuttis, and (speaking of "arm") Ellis and
Stroustrup.
Here's one:
template<size_t _Nb>
class bitset : private _Base_bitset<_GLIBCPP_BITSET_WORDS(_Nb)> {...}
And here's one:
template<bool C, typename T1, typename T2> class IfElse;
template<typename T1, typename T2> class IfElse<true, T1, T2>
{ public: typedef T1 Type; };
template<typename T1, typename T2> class IfElse<false, T1, T2>
{ public: typedef T2 Type; };
And here's one:
template <size_t N, typename T, T val>
class ArrayInitConst
{
public:
static void Apply(T *p)
{
*p++ = val;
ArrayInitConst<N-1,T,val>::Apply(p);
}
};
template <typename T, T val>
class ArrayInitConst<1, T, val>
{
public:
static void Apply(T *p) { *p = val; }
};
Yes, I was asking about non-type template parameters. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: William C. White |
last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using
cURL? Our website is hosted on a shared drive and the webhost company
doesn't installed additional software (such as cURL)...
|
by: Albert Ahtenberg |
last post by:
Hello,
I don't know if it is only me but I was sure that header("Location:url")
redirects the browser instantly to URL, or at least stops the execution of
the code. But appearantely it continues...
|
by: James |
last post by:
Hi,
I have a form with 2 fields.
'A'
'B'
The user completes one of the fields and the form is submitted.
On the results page I want to run a query, but this will change
subject to which...
|
by: Ollivier Robert |
last post by:
Hello,
I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9
system. The link succeeds but everytime I try to run php, I get a SEGV from
inside the libcnltsh.so library.
...
|
by: Richard Galli |
last post by:
I want viewers to compare state laws on a single subject.
Imagine a three-column table with a drop-down box on the top. A viewer
selects a state from the list, and that state's text fills the...
|
by: Albert Ahtenberg |
last post by:
Hello,
I have two questions.
1. When the user presses the back button and returns to a form he filled
the form is reseted. How do I leave there the values he inserted?
2. When the...
|
by: inderjit S Gabrie |
last post by:
Hi all
Here is the scenerio ...is it possibly to do this...
i am getting valid course dates output on to a web which i have designed
....all is okay so far , look at the following web url
...
|
by: Jack |
last post by:
Hi All,
What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g.
select x from y where z=:parameter
Which in asp/jsp would be followed by some statements to bind a value...
|
by: Sandwick |
last post by:
I am trying to change the size of a drawing so they are all 3x3.
the script below is what i was trying to use to cut it in half ... I
get errors.
I can display the normal picture but not the...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |