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? 4 2542
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
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<typena me Tstruct identity { typedef T type; };
template<typena me T>
void do_not_deduce(t ypename identity<T>::ty pe x);
HTH,
--
gpd
On Jun 19, 11:08 am, gpderetta <gpdere...@gmai l.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<typena me Tstruct identity { typedef T type; };
template<typena me T>
void do_not_deduce(t ypename identity<T>::ty pe x);
HTH,
--
gpd
Thank you. This seems to work for me, and is cleaner than my previous
solution.
On Jun 19, 10:08 pm, gpderetta <gpdere...@gmai l.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<typena me Tstruct identity { typedef T type; };
template<typena me T>
void do_not_deduce(t ypename identity<T>::ty pe x);
aha , impressive trick.
HTH,
--
gpd
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 ambigous because of
// odering deduction of the function signature,
|
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 >)
{
}
|
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, and the error I get:
--------------
namespace LIST {
template <typename T> struct Link;
template <typename T> class List;
|
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&);
template<typename T>
|
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); // OK, T = int, i = 10
foo(40,40); // ERROR, T = int but i cannot be deduced.
| |
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 exactly what
the problem is. Is it related to the fact that a nested class is
involved?
#include <iostream>
using std::cout;
|
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_;
|
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 be instantiated. The user does not have
|
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 be instantiated. The user does not have
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |