So, I started my career with C++98, but a lot has changed since then and I've gone back to get myself reaquainted with my old friend. Turns out that my old friend has changed a lot!
At this point I've read through the most recent editions of:
The C++ Programming Language
Effective C++
Modern Effective C++
So, let me lay out what I did, then take a look at the code. I'm happy to hear that I'm being stupid, and would even accept style/convention notes since I haven't had the time to read through any style guides.
What I am looking to do is create a class that contains a static factory method. The factory method will perfect forward to a private constructor. This is to, I believe, force users of my type to always have it wrapped in a std::unique_ptr.
Further I'm pretending that I want to isolate implementation details from my type's users, so I'm applying the PIMPL idiom.
This is obviously a contrived example.
This compiles and works fine with the following compiler:
Here are my questions/points of validation:
At this point I've read through the most recent editions of:
The C++ Programming Language
Effective C++
Modern Effective C++
So, let me lay out what I did, then take a look at the code. I'm happy to hear that I'm being stupid, and would even accept style/convention notes since I haven't had the time to read through any style guides.
What I am looking to do is create a class that contains a static factory method. The factory method will perfect forward to a private constructor. This is to, I believe, force users of my type to always have it wrapped in a std::unique_ptr.
Further I'm pretending that I want to isolate implementation details from my type's users, so I'm applying the PIMPL idiom.
This is obviously a contrived example.
Code:
// Foo.h
#include <memory>
namespace F {
class Foo {
public:
~Foo();
template<typename... Ts>
static std::unique_ptr<Foo> create(Ts&&... params) {
return std::unique_ptr<Foo>(new Foo(
std::forward<decltype(params)>(params)...
));
}
int x();
private:
struct Impl;
std::unique_ptr<Impl> pImpl;
Foo(int x);
};
}
Code:
// Foo.cpp
#include <Foo.h>
using namespace F;
struct Foo::Impl {
int x;
};
// public
Foo::~Foo() = default;
int Foo::x() {
return pImpl->x;
}
// private
Foo::Foo(int x) : pImpl{std::make_unique<Impl>()} {
pImpl->x = x;
}
Code:
// main.cpp
#include <iostream>
#include <memory>
#include "Foo.h"
int main() {
auto f = F::Foo::create(3);
std::cout << "x is: " << f->x() << std::endl;
return 0;
}
This compiles and works fine with the following compiler:
Code:
$ clang++ --version
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Here are my questions/points of validation:
- Do I really need to wrap my pImpl member in a std::unique_ptr? If this were a raw pointer, I suppose I'd have to just provide a deconstructor that deletes it...but that would be it right? I just want to make sure I'm not trading efficiency for not having to write code.
- In Modern Effective C++, it is stated to prefer std::make_unique over the 'new' operator. This makes sense, but in my case I can't use std::make_unique in the Foo::create function because it is trying to forward to a private constructor. I tried, it didn't compile (which makes sense to me). Is this an acceptable place to use new and violate the prefer?
- Based on what I read in Modern Effective C++, it seems to me that the Foo::create function is essentially a specialization of the std::make_unique function. If so, would it make more sense to just specialize std::make_unique? I didn't try this...I'm just speculating. I mean, if my constructor were public then I'd just use std::make_unique. However, that's counter to my concern: I want to force all users of the Foo type to use a std::unique_ptr. The only way I can really do that by making the Foo::create function the only way to construct a Foo. Right? Crazy pants?