Discussion:
Error in normal nested classes inside template classes
(too old to reply)
o***@gmail.com
2018-10-08 21:18:26 UTC
Permalink
Good afternoon.

Could some one help with the following program?

I'm trying to create a function to receive a nested class of a template
class as parameter (normal function parameter), but the outer class is a
template of type T, so I made the function a template function to
generalize the use, but doesn't compile! As the outer class is a template
class, but if just the inner class is a template class the program compiles
and works just fine. I can even use the type
TemplateClass<someType>::innerClass normally as any other type and compiles.

I think it's a bug, because I separated it 4 compiling scenarios and it
works fine in the case I don't use templates in the function to pass a
specific type of the nested class of the template class. Look at the code,
I hope you will understand:

#include<iostream>
using std::cout;

//Compiling with: g++ -std=c++17 Template_error.cpp -o Template_error.run

#define compilation_scenery 0

//################################
//########## Works fine: ###########
//################################

struct Gama{

template<typename> struct Teta{};
};

template< typename T>
void function1( Gama::Teta<T> x){ cout << "Passed Gama::Teta<T> type.\n";}

//################################
//######### Compile errors: #########
//################################

template<typename> struct Alfa{

struct Beta{};
};

#if compilation_scenery == 1 //Doesn't compile

template< typename T>
void function2( Alfa<T>::Beta x){ cout << "Passed Alfa<T>::Beta
type.\n";}

#elif compilation_scenery == 2 //Doesn't compile (the error message is
different)

template< typename T>
int function2( Alfa<T>::Beta x){ cout << "Passed Alfa<T>::Beta
type.\n"; return 0;}

#elif compilation_scenery == 3

void function2( Alfa<bool>::Beta x){ cout << "Passed Alfa<bool>::Beta
type.\n";} //IT ALSO WORKS TOO!!!!

#endif

//################################
//############# Main ##############
//################################

int main(){

Gama::Teta<bool> x1; //Works fine always...
function1(x1); //Works fine always...

Alfa<bool>::Beta x2; //It also always works!

#if compilation_scenery >= 1

function2(x2); //Fails in compilation scenery 1 and 2...

#endif

return 0;}

I'm compiling with g++ using -std=c++17 on Linux Ubuntu 16.04 LTS updated.
If you want to try by yourself copy and paste this code and compile with
the compilation_scenery varying from 0 to 3.
Thank you for your help!

P.S: if it's really a bug on C++ template system, should I submit the bug
by this site: https://isocpp.org/std/submit-issue ?
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Hyman Rosen
2018-10-08 21:44:48 UTC
Permalink
You have three errors.

First, in declaring

template<typename T> void function2(Alfa<T>::Beta x)

you must use typename or Beta is assumed to be a non-type name:

template<typename T> void function2(*typename* Alfa<T>::Beta x)

Second, the template parameter T above is in a non-deduced context. That
is,
given an argument of type Alfa<SomeType>::Beta, the compiler will not deduce
that the template parameter T is SomeType. The fundamental reason is that
there
can be other specializations of Alfa that define the same Beta:

template <> struct Alfa<int> { typedef Alfa<char>::Beta Beta; };
template <> struct Alfa<unsigned> { typedef Alfa<int>::Beta Beta; };

Third, this is not the correct place to request help with programming
problems.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Richard Hodges
2018-10-09 10:23:00 UTC
Permalink
Post by Hyman Rosen
You have three errors.
First, in declaring
template<typename T> void function2(Alfa<T>::Beta x)
template<typename T> void function2(*typename* Alfa<T>::Beta x)
Second, the template parameter T above is in a non-deduced context. That
is,
given an argument of type Alfa<SomeType>::Beta, the compiler will not deduce
that the template parameter T is SomeType. The fundamental reason is
that there
template <> struct Alfa<int> { typedef Alfa<char>::Beta Beta; };
template <> struct Alfa<unsigned> { typedef Alfa<int>::Beta Beta; };
Third, this is not the correct place to request help with programming
problems.
For reference, this is generally agreed (citation needed) to be a good
place to post such questions:
https://stackoverflow.com/questions/tagged/c%2b%2b
Post by Hyman Rosen
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Continue reading on narkive:
Loading...