c++ - Private Class within Namespace -
i have class within namespace in header file. class requires template type, , want types used. below shows example.
file a.hpp
// a.hpp namespace a_ns { template<class t> class { // stuff }; typedef a<double> a_double; } // end of namespace // stuff
file b.hpp
// b.hpp #include <a.hpp> namespace b_ns { typedef a_ns::a_double b; }
file main.cpp
// main.cpp #include "b.hpp" int main() { b_ns::b my_b; // <<<--- this! a_ns::a<float> my_a_which_is_not_allowed; // <<<--- not though! d: }
so can see rather longed out example, end objective not allow end user declare class a
float
typename, , able use pre-defined classes specific types, declared typedef a<double> a_double;
.
i thought example above allow this, wrong, can create a<float>
above, because include b.hpp
, in turn includes a.hpp
! see problem! (hopefully?)
there simple solution, if @ possible.
if want able use type aliases , not use a
directly, can put implementation namespace users should know not use:
namespace a_ns { namespace detail { template<class t> class { // stuff }; } typedef detail::a<double> a_double; } // end of namespace
now can use a_double
, use a
directly, detail
namespace have dug into, , that's accepted bad thing do. if user decides want that, they've given on staying out of trouble , shouldn't take measures stop them hurting themself.
Comments
Post a Comment