* Re: Is there a way to test for visibility
@ 2009-11-15 18:32 Daniel Walter
2009-11-16 0:34 ` Stuck trying to do first exercise in C++ Template Metaprogramming Patrick Horgan
0 siblings, 1 reply; 4+ messages in thread
From: Daniel Walter @ 2009-11-15 18:32 UTC (permalink / raw)
To: gcc-help
Is there a way to test the current visibility status with a #if condition?
I am trying to figure out where visibility hidden is coming from and would
like to put something to the effect of
#if visibility==hidden
#error
#endif
in my code to try to track this down.
Daniel
^ permalink raw reply [flat|nested] 4+ messages in thread
* Stuck trying to do first exercise in C++ Template Metaprogramming
2009-11-15 18:32 Is there a way to test for visibility Daniel Walter
@ 2009-11-16 0:34 ` Patrick Horgan
2009-11-16 12:45 ` John (Eljay) Love-Jensen
0 siblings, 1 reply; 4+ messages in thread
From: Patrick Horgan @ 2009-11-16 0:34 UTC (permalink / raw)
Cc: gcc-help
I'm reading through C++ Template Metaprogramming and thought I was
understanding things pretty well until I tried to do exercise 2-0 which
wants you to make a add_const_ref<T> that returns T if it's already a
reference type, else return T const&. I include my floundering below
inline, which gets these errors in their current state. I'm really stuck.
g++ -ggdb test.cpp -o test
In file included from test.cpp:1:
add_const_ref.h:21: error: explicit specialization in non-namespace
scope ‘class mine::add_const_ref<T>’
add_const_ref.h:22: error: template parameters not used in partial
specialization:
add_const_ref.h:22: error: ‘T’
#if !defined ADD_CONST_REF
#define ADD_CONST_REF
#include <boost/type_traits/add_reference.hpp>
#include <boost/type_traits/is_reference.hpp>
namespace mine {
template <bool is_const_ref>struct acr_impl;
template<typename T>
class add_const_ref
{
bool static const is_const_ref=boost::is_reference<T>::value;
template<bool x>
struct acr_impl
{
typedef T type;
};
template<>
struct acr_impl<true>
{
typedef T type;
};
#if 0
template<>
struct acr_impl<false>
{
typedef boost::add_reference<T> type;
};
typedef acr_impl<is_const_ref>::type type;
#endif
};
};
#endif // ADD_CONST_REF
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Stuck trying to do first exercise in C++ Template Metaprogramming
2009-11-16 0:34 ` Stuck trying to do first exercise in C++ Template Metaprogramming Patrick Horgan
@ 2009-11-16 12:45 ` John (Eljay) Love-Jensen
2009-11-20 22:29 ` Patrick Horgan
0 siblings, 1 reply; 4+ messages in thread
From: John (Eljay) Love-Jensen @ 2009-11-16 12:45 UTC (permalink / raw)
To: phorgan1; +Cc: GCC-help
Hi Patrick,
To simplify the problem, what you are basically trying to do is this:
------------------------------
struct Foo
{
template <bool x>
struct MyTemplate
{
enum { test = false };
};
template <>
struct MyTemplate<true>
{
enum { test = true };
};
};
------------------------------
The MyTemplate<true> template specialization cannot be done inside the Foo
declaration. It is not valid C++.
Instead, you should do the template specialization outside of the declaring
outer struct (or in your case, outside of the outer template class
declaration) like this:
------------------------------
struct Foo
{
template <bool x>
struct MyTemplate
{
enum { test = false };
};
};
template <>
struct Foo::MyTemplate<true>
{
enum { test = true };
};
------------------------------
Hope that helps,
--Eljay
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Stuck trying to do first exercise in C++ Template Metaprogramming
2009-11-16 12:45 ` John (Eljay) Love-Jensen
@ 2009-11-20 22:29 ` Patrick Horgan
0 siblings, 0 replies; 4+ messages in thread
From: Patrick Horgan @ 2009-11-20 22:29 UTC (permalink / raw)
To: John (Eljay) Love-Jensen; +Cc: GCC-help
John (Eljay) Love-Jensen wrote:
> ------------------------------
> struct Foo
> {
> template <bool x>
> struct MyTemplate
> {
> enum { test = false };
> };
> };
>
> template <>
> struct Foo::MyTemplate<true>
> {
> enum { test = true };
> };
> ------------------------------
>
> Hope that helps,
>
Yes, thank you, it came out like this:
template<typename T,bool> struct acr_impl;
template<typename T> struct acr_impl<T,true> { typedef T type; };
template<typename T> struct acr_impl<T,false> { typedef T& type; };
template<typename T>
struct add_const_ref
{
typedef typename acr_impl<T,boost::is_reference<T>::value>::type type;
};
and works fine:)
Patrick
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2009-11-20 22:29 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-15 18:32 Is there a way to test for visibility Daniel Walter
2009-11-16 0:34 ` Stuck trying to do first exercise in C++ Template Metaprogramming Patrick Horgan
2009-11-16 12:45 ` John (Eljay) Love-Jensen
2009-11-20 22:29 ` Patrick Horgan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).