473,394 Members | 1,702 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,394 software developers and data experts.

Turning off template parameter deduction

KD
I have a template function and I'm looking for a way to force the
caller to specify the template parameters. In other words, I would
like to turn off template parameter deduction. For example,

template <class T>
void foo(T t)
{
...
}

foo(123); // I want this to fail.
foo<int>(123); // I want this to succeed.

I was hoping that the explicit keyword would work, but it didn't. So
far, the only half working hack that I came up with is having two
parameters S and T, where S is not used so it must be specified. In
the method, I then do static checks that T is convertible to S, but it
doesn't work in all cases and it adds some complexity to the code.

Any ideas?
Jun 27 '08 #1
4 2520
KD wrote:
I have a template function and I'm looking for a way to force the
caller to specify the template parameters. In other words, I would
like to turn off template parameter deduction. For example,

template <class T>
void foo(T t)
{
...
}

foo(123); // I want this to fail.
foo<int>(123); // I want this to succeed.

I was hoping that the explicit keyword would work, but it didn't. So
far, the only half working hack that I came up with is having two
parameters S and T, where S is not used so it must be specified. In
the method, I then do static checks that T is convertible to S, but it
doesn't work in all cases and it adds some complexity to the code.

Any ideas?
the more important question to ask is: why would you want to do
something like that?

Although I believe there are ways to do what you wanted but I don't
think there is a generic solution.

F
Jun 27 '08 #2
On Jun 19, 3:51*pm, Fei Liu <fei....@gmail.comwrote:
KD wrote:
I have a template function and I'm looking for a way to force the
caller to specify the template parameters. *In other words, I would
like to turn off template parameter deduction. *For example,
template <class T>
void foo(T t)
{
* * ...
}
foo(123); *// I want this to fail.
foo<int>(123); // I want this to succeed.
I was hoping that the explicit keyword would work, but it didn't. *So
far, the only half working hack that I came up with is having two
parameters S and T, where S is not used so it must be specified. *In
the method, I then do static checks that T is convertible to S, but it
doesn't work in all cases and it adds some complexity to the code.
Any ideas?

the more important question to ask is: why would you want to do
something like that?
To implement something similar to standard C++ casts for example.
>
Although I believe there are ways to do what you wanted but I don't
think there is a generic solution.
Sure that there is: just put T in a non deducible context:

template<typename Tstruct identity { typedef T type; };

template<typename T>
void do_not_deduce(typename identity<T>::type x);

HTH,

--
gpd

Jun 27 '08 #3
KD
On Jun 19, 11:08 am, gpderetta <gpdere...@gmail.comwrote:
On Jun 19, 3:51 pm, Fei Liu <fei....@gmail.comwrote:
KD wrote:
I have a template function and I'm looking for a way to force the
caller to specify the template parameters. In other words, I would
like to turn off template parameter deduction. For example,
template <class T>
void foo(T t)
{
...
}
foo(123); // I want this to fail.
foo<int>(123); // I want this to succeed.
I was hoping that the explicit keyword would work, but it didn't. So
far, the only half working hack that I came up with is having two
parameters S and T, where S is not used so it must be specified. In
the method, I then do static checks that T is convertible to S, but it
doesn't work in all cases and it adds some complexity to the code.
Any ideas?
the more important question to ask is: why would you want to do
something like that?

To implement something similar to standard C++ casts for example.
Although I believe there are ways to do what you wanted but I don't
think there is a generic solution.

Sure that there is: just put T in a non deducible context:

template<typename Tstruct identity { typedef T type; };

template<typename T>
void do_not_deduce(typename identity<T>::type x);

HTH,

--
gpd
Thank you. This seems to work for me, and is cleaner than my previous
solution.
Jun 27 '08 #4
On Jun 19, 10:08 pm, gpderetta <gpdere...@gmail.comwrote:
On Jun 19, 3:51 pm, Fei Liu <fei....@gmail.comwrote:
KD wrote:
I have a template function and I'm looking for a way to force the
caller to specify the template parameters. In other words, I would
like to turn off template parameter deduction. For example,
template <class T>
void foo(T t)
{
...
}
foo(123); // I want this to fail.
foo<int>(123); // I want this to succeed.
I was hoping that the explicit keyword would work, but it didn't. So
far, the only half working hack that I came up with is having two
parameters S and T, where S is not used so it must be specified. In
the method, I then do static checks that T is convertible to S, but it
doesn't work in all cases and it adds some complexity to the code.
Any ideas?
the more important question to ask is: why would you want to do
something like that?

To implement something similar to standard C++ casts for example.
IMO, to implement cast like things doesn't need the trick.

template <typename T, typename ST& user_cast(S& s);

T is result type, can't be deduced yet.
>
Although I believe there are ways to do what you wanted but I don't
think there is a generic solution.

Sure that there is: just put T in a non deducible context:

template<typename Tstruct identity { typedef T type; };

template<typename T>
void do_not_deduce(typename identity<T>::type x);
aha , impressive trick.
HTH,

--
gpd
Jun 27 '08 #5

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

Similar topics

3
by: Chris Johnson | last post by:
Greetings all: I come across an interesting question (to me anyway) and I do not know the answer. Code/Questions follow: #include <iostream> #if 0 // uncommenting *should* make call...
3
by: BigMan | last post by:
Here is a piece of code: #include <memory> using namespace std; template< typename SomeType > void f(auto_ptr_ref< SomeType >) { }
4
by: Mat DeLong | last post by:
I have never been stuck on programming something before to the point I give up... this is a first. I am programming what should be something very easy in C++... using Templates. Here is the code,...
14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
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);...
2
by: coolpint | last post by:
Can anyone kindly provide an explanation as to why the compiler does not "see" the function template in the contrieved code below? I think the argument deduction fails but can't figure out...
7
by: Dilip | last post by:
This is just an academic question but is there any particular reason why this does not work? template<typename T> class Foo { public: Foo(T x) : myvar_(x) { } private: T myvar_;
0
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto...
3
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.