Hi,
I want to distinguish between an int parameter to a template function
depending on whether its value is known at compile time or not. For the
compile time version I want to call the function with the syntax
func<5>();
and for the dynamic version with
func(5);
This can be simply achieved by function overloading through the two function
declarations:
template <int ivoid func() { ... }
void func(int i) {... }
The same thing I want to combine with argument dependent lookup (ADL).
Therefore the functions are declared inside a namespace for example "test"
and both get an argument of a templated typename T:
namespace test {
template <int i, typename Tvoid func(const T&) {...}
template <typename Tvoid func(int i, const T&) {...}
}
If we now declare a struct A inside the same namespace:
namespace test {
struct A { ... };
}
ADL should allow us to use func on an instance of A in the main function
without specifying the namespace test:
void main(int,char** ) {
test::A a;
func<2>(a); // error under visual studio 8 (.Net 2005)
func(2,a); // this is fine
}
In this code the ADL does not work for the compile time version, where the
int is passed as template argument. Now my questions:
1. Is this a bug of the Visual Studio 8 compiler or is this use of ADL not
intended by the standard?
2. Has anyone a good idea of circumvent this problem without messing up the
notation too much?
In order to let you try this out on other compilers here is a complete
example:
#include <iostream>
namespace test
{
template <int i, typename T>
void func_a(const T& t)
{
std::cout << "called func with t = " << t << " and i = " << i <<
std::endl;
}
template <typename T>
void func_b(int i, const T& t)
{
std::cout << "called func with t = " << t << " and i = " << i <<
std::endl;
}
struct A
{
int i;
A(int _i) : i(_i) {}
friend std::ostream& operator << (std::ostream& os, const A& a)
{
return os << a.i;
}
};
}
void main(int, char**)
{
test::A a(3);
func_a<1>(a); // error
func_a<1,test:: A>(a); // error
test::func_a<1> (a); // this is fine but does not help
func_b(1, a); // this works perfect
} 2 2043
cgv wrote:
1. Is this a bug of the Visual Studio 8 compiler or is this use of ADL not
intended by the standard?
VC8 is correct. I would say that rather than not intended its a
problem.
The compiler doesnt know what func is outside its namespace , so it
might be a variable as it parses it e.g:
func < 2
etc.
2. Has anyone a good idea of circumvent this problem without messing up the
notation too much?
One option is to provide a type:
#include <iostream>
namespace my{
template <int Nstruct dummy_int{};
}
//Now use :
namespace my{
template <int N>
void func( dummy_int<N>)
{
std::cout << " N = " << N << '\n';
}
}
int main()
{
func(my::dummy_ int<2>());
func(my::dummy_ int<1000>());
}
regards
Andy Little
cgv schrieb:
In order to let you try this out on other compilers here is a complete
example:
[...]
>
void main(int, char**)
int main (int, char**)
[...] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Patrick Kowalzick |
last post by:
Dear all,
I have a question about default template parameters.
I want to have a second template parameter which as a default parameter,
but depends on the first one (see below). Is something like that possible?
Some workaround?
Thank you,
Patrick
|
by: Dave |
last post by:
#include <iostream>
using std::cout;
using std::endl;
// #define CAUSE_ERROR
namespace N
{
struct foo_t {};
|
by: REH |
last post by:
I'm a little confused about argument dependent lookup. Exactly when does
this apply? Specifically, I was hoping to use it to access enumeration
constants. For example:
namespace Flags {
enum flag_type {F1, F2, F3};
}
void foo(Flags::flag_type f)
{
|
by: Jean-Louis Leroy |
last post by:
I'm trying to compile the following code with gcc (version below). I
would expect the compile to try the operator ==() declared in
Tangram::Backend because of Koenig lookup. But it doesn't happen. Is
it me or the compiler?
namespace Tangram {
namespace Backend {
struct RELATIONAL;
|
by: REH |
last post by:
I'm a little confused about argument dependent lookup. Exactly when does
this apply? Specifically, I was hoping to use it to access enumeration
constants. For example:
namespace Flags {
enum flag_type {F1, F2, F3};
}
void foo(Flags::flag_type f)
{
| |
by: Javier Estrada |
last post by:
I'm using a user-defined diagnostics stream and custom manipulators that work with it
template<class charT, class traits = std::char_traits<charT>
class basic_diagsstream : public std::basic_ostream<charT, traits
...
}
and a manipulator
|
by: mathieu |
last post by:
Hello,
I was playing with template programing and I am struggling with the
following (*):
float f3 = foo((TypeToType<0>::Type*)0);
where foo is declared as:
template <int T>
|
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 to call
a method defined in A's base class which at runtime determines the
template parameters (I know ahead what is allowed) and calls a
templated member function B with A's template parameters.
|
by: Juha Nieminen |
last post by:
The so-called koenig lookup allows doing odd things like this:
#include <algorithm>
#include <string>
int main()
{
std::string table;
sort(table, table+10);
}
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |