gpt4 book ai didi

c++ - 从 Class 调用 Class 的私有(private)构造函数

转载 作者:行者123 更新时间:2023-11-30 01:15:52 27 4
gpt4 key购买 nike

下面是我的代码的简化版本。

#include <vector>
#include <algorithm>

template <typename T> struct Foo {
using Value = T;
constexpr Foo() = delete;
constexpr Foo(T v) : value(v) {}
T value;
};

template <typename T> struct Vec {
using Elem = Foo<T>;
using Container = std::vector<Elem>;

Vec() = delete;
template <typename... Elems>
Vec(Elem a, Elem b, Elems... rest)
: elems_{a, b, rest...} {}

void add(const Elem &e) { elems_.push_back(e); }

template <typename F> auto map(const F &f) const;

private:
Vec(Container &&c) : elems_(std::move(c)) {}
Container elems_;
};

template <typename C>
template <typename F>
auto Vec<C>::map(const F &f) const {
using ReturnedFoo = decltype(f(std::declval<typename Vec<C>::Elem>()));
using ValueType = typename ReturnedFoo::Value;
using Container = typename Vec<ValueType>::Container;
Container mapped_elems;
mapped_elems.reserve(elems_.size());
std::transform(elems_.begin(), elems_.end(), std::back_inserter(mapped_elems),
f);
return Vec<ValueType>{std::move(mapped_elems)};
}

Foo<int> mul2(Foo<int> x) {
return Foo<int>{2 * x.value};
}
Foo<double> to_d(Foo<int> x) {
return Foo<double>{static_cast<double>(x.value)};
}

int main() {
constexpr auto f1 = Foo<int>(1);
constexpr auto f2 = Foo<int>(2);
constexpr auto f3 = Foo<int>(3);
const auto v1 = Vec<int>{f1, f2, f3};
const auto v2 = v1.map(mul2);
// const auto v3 = v1.map(to_d); // call to private constructor from here
}

我的类(class) Vec<T>内部持有 Foo<T> 类型的元素在 std::vector并且它始终包含至少 2 个元素。

我写了一个map映射/转换每个元素并返回新的 Vec 的函数目的。对于不更改元素类型( F : (T) -> T )的映射函数,一切正常,例如 mul2 .但是一般情况 ( F : (T) -> U ) 不起作用,因为类 Vec<U> 中有对私有(private)构造函数的调用。来自 Vec<T>::map功能。公开此构造函数可以创建 Vec少于 2 个元素的对象,所以这不是我想要的。

我第一次尝试解决这个问题是

  template <typename X> friend class Vec<X>;

但好像是不允许的。这是 clang++ 的输出:

vec.cpp:23:14: error: partial specialization cannot be declared as a friend
friend class Vec<X>;

有没有办法让它发挥作用?在写这个问题的过程中,我想到了将 map 的最后一行重写为:

  Vec<ValueType> result{mapped_elems[0], mapped_elems[1]};
for (auto it = mapped_elems.begin() + 2 ; it != mapped_elems.end(); ++it) {
result.add(*it);
}
return result;

但是还有别的办法吗?

(顺便说一句,我有 clang 3.5.0 和 g++ 4.9.2)

最佳答案

如果你想要 Vec 的任何特化成为当前的 friend Vec<T>实例化, friend 声明应该是:

template <typename X> friend struct Vec;
// ^ ok, plain identifier

代替:

template <typename X> friend class Vec<X>;
// ^~~ wrong!

§11.3 [class.friend]/p3:

A friend declaration that does not declare a function shall have one of the following forms:

friend elaborated-type-specifier ;
friend simple-type-specifier ;
friend typename-specifier ;

[ Note: A friend declaration may be the declaration in a template-declaration (Clause 14, 14.5.4).— end note ]

您当前使用的语法与 Vec 的部分特化声明相匹配,这是 §14.5.4 [temp.friend]/p8 禁止的:

Friend declarations shall not declare partial specializations.

关于c++ - 从 Class<U> 调用 Class<T> 的私有(private)构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27754396/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com