473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Template parameters for template function

Hello All!

There is following code:

template < class T >
void f(T const & value)
{
};

Is there any way to force user to specify template parameters
explicitly? I want to do the following:
f<int>(1); // Normal
f(1); // This should give an error

Any ideas?...

WBR

Apr 12 '07 #1
4 1366
ru**********@gmail.com wrote:
Hello All!

There is following code:

template < class T >
void f(T const & value)
{
};

Is there any way to force user to specify template parameters
explicitly? I want to do the following:
f<int>(1); // Normal
f(1); // This should give an error

Any ideas?...

Try this:
template < class T >
struct f_helper
{
typedef T type;
};
template < class T >
void f(typename f_helper<T>::type const & value)
{
};

The compiler is unable to deduce the type T from f_helper<T>::type.
Apr 12 '07 #2
Hi

ru**********@gmail.com wrote:
Is there any way to force user to specify template parameters
explicitly? I want to do the following:
f<int>(1); // Normal
f(1); // This should give an error

Any ideas?...
You would have to make T undeducible. (word sounds wrong?)
You could for example use an indirection:

template<typename T>
struct f_helper
{
const T& value;
f_helper(const T& value) : value(value) {}
operator const T& () const { return value; }
};

template<typename T>
void f(const f_helper<T>& h)
{
const T& value = h;
// ...
}

Then f(1) fails to compile, while f<int>(1) will still compile.

Markus
Apr 12 '07 #3
Gianni Mariani:
ru**********@gmail.com wrote:
Hello All!

There is following code:

template < class T >
void f(T const & value)
{
};

Is there any way to force user to specify template parameters
explicitly? I want to do the following:
f<int>(1); // Normal
f(1); // This should give an error

Any ideas?...


Try this:
template < class T >
struct f_helper
{
typedef T type;
};
template < class T >
void f(typename f_helper<T>::type const & value)
{
};

The compiler is unable to deduce the type T from f_helper<T>::type.
Thanks, it works!...

Apr 12 '07 #4
Markus Moll wrote:
>
You would have to make T undeducible. (word sounds wrong?)
The standard doesn't use the word "deducible," so there's no direct help
there. But it does use the word "deduced" as in "deduced context", and
it uses the word "non-deduced" as its opposite.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Apr 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: wkaras | last post by:
I would like to propose the following changes to the C++ Standard, the goal of which are to provide an improved ability to specify the constraints on type parameters to templates. Let me say from...
4
by: Neelesh | last post by:
Hi all, I had some confusion about deduction of non-type template parameters for function templates : template <class T, int i> void foo (T, int p = i) ; void bar() { foo<int, 10>(40,40);...
3
by: Erik Wikström | last post by:
I've been trying for a while now to understand how template template parameters work. But I just can't wrap my head around it and was hoping that someone might help me. As best I can figure the...
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
4
by: Dan Krantz | last post by:
I have the following template to ensure that a given number (val) falls into a range (between vmin & vmax): template<typename T> T ForceNumericRange( const T& val, const T& vmin, const T& vmax)...
12
by: stefan.bruckner | last post by:
Hi, I am looking for a way to achieve the following. I've tried a couple of things, but they all ended up being too complicated: I have a templated class A. I want another class B to be able...
8
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
3
by: gary.bernstein | last post by:
I want to call a singleton getInstance function to retrieve a templatized object without knowing what types were used to create the singleton object in the first call to getInstance. How can I do...
2
by: Pierre Yves | last post by:
Hi there, Sorry for the double subject but I feel they are related. I'm not pretty sure there would be an answer but I reckon there must be a way to make it work. I would like to write the...
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
BarryA
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.