Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

create a class using null type?

34 views
Skip to first unread message

Joe

unread,
Jun 11, 2015, 12:39:02 PM6/11/15
to
I'd like to know if it's possible to initialize a class w/o passing in a
type to the template, just use the base class since I don't need any of
the template members. I thought I read that passing in null pointers was
ok, but didn't quite understand the const expression part of it.

template <class MyType>
class MyClass
{
private:
std::shared_ptr<MyType> tvar; // <---- don't need to use

public:
MyClass() { } // <--------- use this ctor
MyClass(std::shared_ptr<MyType> myT) : tvar(myT) { }
};

So to create a simple object of MyClass, I tried something like this.

std::unique_ptr<MyType<nullptr>> mt;
mt = std::unique_ptr<MyType<nullptr>> { new MyClass(); };

Victor Bazarov

unread,
Jun 11, 2015, 1:03:06 PM6/11/15
to
On 6/11/2015 12:39 PM, Joe wrote:
> I'd like to know if it's possible to initialize a class w/o passing in a
> type to the template,

No. Templates are *instantiated* when their *arguments* have specific
values. If the argument is a type, it needs to be specified in order to
instantiate the template.

> just use the base class since I don't need any of
> the template members.

By definition, you do. You're free to rewrite your program in such a
way that it doesn't have a template, and instead has a class which you
then use. But a class template *needs* its argument to become a class.

> I thought I read that passing in null pointers was
> ok, but didn't quite understand the const expression part of it.

Huh?

>
> template <class MyType>
> class MyClass
> {
> private:
> std::shared_ptr<MyType> tvar; // <---- don't need to use
>
> public:
> MyClass() { } // <--------- use this ctor
> MyClass(std::shared_ptr<MyType> myT) : tvar(myT) { }
> };
>
> So to create a simple object of MyClass, I tried something like this.
>
> std::unique_ptr<MyType<nullptr>> mt;
> mt = std::unique_ptr<MyType<nullptr>> { new MyClass(); };
>

This is wrong on several levels. Perhaps you could start over by
explaining what exactly you're trying to accomplish.

V
--
I do not respond to top-posted replies, please don't ask

Öö Tiib

unread,
Jun 11, 2015, 1:06:11 PM6/11/15
to
'MyType<nullptr>' can't compile. 'MyType' expects a type as
template argument but 'nullptr' is a keyword that denotes a
value not type.

Perhaps you did mean 'MyType<std::nullptr_t>'?

Joe

unread,
Jun 11, 2015, 3:25:11 PM6/11/15
to

I have a template that I instantiate fine when passing a class type. I
understand the template needs a type to instantiate a class. What I'm
wondering is how to instantiate a template by passing a null because in
one situation, I only need to make use of a few methods of the class
that don't use the passed in template type. In otherwords, I'd like to
use it as a normal class and thought maybe I could just pass in a null
as the template arg.

Here's my latest try ...

std::unique_ptr<MyClass<std::nullptr_t>> mc;

mc = std::unique_ptr<MyClass<std::nullptr_t>> {
new MyClass<std::nullptr_t>(nullptr);
};
Message has been deleted

Richard

unread,
Jun 12, 2015, 6:45:05 PM6/12/15
to
[Please do not mail me a copy of your followup]

Joe <no....@noemail.com> spake the secret code
<mlcdg...@news7.newsguy.com> thusly:
This works for me:

#include <memory>

template <class MyType>
class MyClass
{
private:
std::shared_ptr<MyType> tvar;

public:
MyClass() {}
MyClass(std::shared_ptr<MyType> myT) : tvar(myT) {}
};

int main()
{
MyClass<void> v;
return 0;
}

...although the example provided feels pedantic.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
0 new messages