我有一个类必须依赖于int模板参数的某些原因.
出于同样的原因,该参数不能成为类的参数列表的一部分,而是它的构造函数的参数列表的一部分(当然,模板化).
这里出现了问题.
也许我错过了一些东西,但是我看不到向构造函数提供这样一个参数的简单方法,因为它不能推断也不能明确指定.
到目前为止,我发现了以下替代方案:
将上述参数放入类的参数列表中
创建工厂方法或工厂函数,可以作为示例调用 factory<42>(params)
为构造函数提供traits结构
我试图为最后提到的解决方案创建一个(不那么)最小的工作示例,以便更好地解释问题.
示例中的类不是自身的模板类,因为关键点是构造函数,无论如何真正的模板类是模板类.
#include<iostream>
#include<array>
template<int N>
struct traits {
static constexpr int size = N;
};
class C final {
struct B {
virtual ~B() = default;
virtual void foo() = 0;
};
template<int N>
struct D: public B{
void foo() {
using namespace std;
cout << N << endl;
}
std::array<int, N> arr;
};
public:
template<typename T>
explicit C(T) {
b = new D<T::size>{};
}
~C() …Run Code Online (Sandbox Code Playgroud) 此代码有效:
std::ifstream f(mapFilename.c_str());
std::string s = std::string(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
ParseGameState(s);
Run Code Online (Sandbox Code Playgroud)
因此mapFilename是std::string和void ParseGameState(const std::string&);.
而这不是:
std::ifstream f(mapFilename.c_str());
std::string s(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
ParseGameState(s);
Run Code Online (Sandbox Code Playgroud)
这是错误:
game.cpp: In member function ‘int Game::LoadMapFromFile(const std::string&)’:
game.cpp:423: error: no matching function for call to ‘ParseGameState(std::string (&)(std::istreambuf_iterator<char, std::char_traits<char> >, std::istreambuf_iterator<char, std::char_traits<char> > (*)()))’
game.cpp:363: note: candidates are: ParseGameState(const std::string&)
Run Code Online (Sandbox Code Playgroud)
所以它似乎s在这种情况下识别为函数声明而不是变量声明.
这是为什么?这是GCC 4.2.1(Apple版本)中的错误吗?或者GCC是否正确处理了这个问题?这在C++标准中是否未定义?
我一直试图从这里std::reference_wrapper理解 的实现,如下:
namespace detail {
template <class T> constexpr T& FUN(T& t) noexcept { return t; }
template <class T> void FUN(T&&) = delete;
}
template <class T>
class reference_wrapper {
public:
// types
typedef T type;
// construct/copy/destroy
template <class U, class = decltype(
detail::FUN<T>(std::declval<U>()),
std::enable_if_t<!std::is_same_v<reference_wrapper, std::remove_cvref_t<U>>>()
)>
constexpr reference_wrapper(U&& u) noexcept(noexcept(detail::FUN<T>(std::forward<U>(u))))
: _ptr(std::addressof(detail::FUN<T>(std::forward<U>(u)))) {}
reference_wrapper(const reference_wrapper&) noexcept = default;
// assignment
reference_wrapper& operator=(const reference_wrapper& x) noexcept = default;
// access
constexpr operator T& () const …Run Code Online (Sandbox Code Playgroud) 这个问题似乎与以下内容密切相关,但我还没有完全理解它。
我想要一个模板类,它在构造函数中做一些重要的事情,这取决于另一个模板类型。一个最小的例子是这里:
template <typename A>
class Class {
public:
template <typename B>
Class();
private:
int i;
};
template <typename A, typename B>
Class<A>::Class() {
B b;
i = b.get_number();
}
Run Code Online (Sandbox Code Playgroud)
这不能用 GCC 编译:
$ env LC_ALL=C g++ --std=c++11 -c template.cpp
template.cpp:14:1: error: prototype for 'Class<A>::Class()' does not match any in class 'Class<A>'
Class<A>::Class() {
^~~~~~~~
template.cpp:7:5: error: candidate is: template<class A> template<class B> Class<A>::Class()
Class();
^~~~~
Run Code Online (Sandbox Code Playgroud)
使用 Clang 编译会出现其他错误:
$ env LC_ALL=C clang++ --std=c++11 -c template.cpp …Run Code Online (Sandbox Code Playgroud) c++ ×4
templates ×3
constructor ×1
declaration ×1
decltype ×1
declval ×1
function ×1
traits ×1
variables ×1