我试图触及C++ 17的功能,我选择了clang.这是我的代码的简化示例,无法通过clang编译:
#include <iostream>
#include <limits>
template<
typename T,
template<typename T_> typename Final>
class Base
{
public:
decltype(auto) Foo() const noexcept
{
using TFinal = Final<T>;
auto& _this = static_cast<const TFinal&>(*this);
return _this.Bar<true>();
}
};
template<typename T>
class Derived :
public Base<T, ::Derived>
{
public:
template<bool min>
T Bar() const noexcept
{
return min ?
std::numeric_limits<T>::lowest() :
std::numeric_limits<T>::max();
}
};
int main()
{
Derived<int> instance;
auto result = instance.Foo();
std::cout << result << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它失败了:
return _this.Bar<true>(); …Run Code Online (Sandbox Code Playgroud) 尝试创建包含 std::map 元素的模板函数时,会出现此错误。我知道地图有四个模板参数,但两个有默认值,无法理解我必须做什么。
template<typename key, typename val> void arr_out (std::map<key, val>::iterator begin, std::map<key, val>::iterator end)
{
std::cout << "map: " << std::endl;
while(begin != end)
{
std::cout << (*begin).first << ": " << (*begin).second << std::endl ;
begin++;
}
std::cout << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
如何lpNorm使用模板化矩阵作为输入进行调用?
这是我的代码中的问题:
template<class number>
void normalize_in_place(Matrix<number,Dynamic,Dynamic> & A, level l, axis a){
const int p = lp_norms::level2pvalue(l);
switch ( a ) {
case row:
// TODO: get lpnorm working.
A = A.transpose().cwiseQuotient(A.rowwise().lpNorm<p>()).transpose();
//A = A.transpose().cwiseQuotient(A.rowwise().norm()).transpose();
break;
case col:
// TODO: get lpnorm working.
//A = A.cwiseQuotient(A.colwise().lpNorm<p>());
A = A.cwiseQuotient(A.colwise().norm());
break;
}
}
Run Code Online (Sandbox Code Playgroud)
编译期间失败:
错误:[ lpNorm 函数的右括号] 之前的预期主表达式
我用括号替换了您在编译器输出中看到的箭头。
** 用 7 月 23 日的版本刷新我的特征后,我收到以下错误:
错误:类型为 '' 和 'const int' 的无效操作数转换为二进制 'operator<' A = A.transpose().cwiseQuotient(A.rowwise().lpNorm
()).转置();
显然,lpNorm没有解决;但是,我已经包括 …
我一直在查看 vulkan-hpp 源代码,试图了解如何管理StructureChains。我发现这种看起来很奇怪的语法(用注释标记的行)与template关键字作为成员类型的使用有关。此外,它后面是一个没有;前面的函数调用。
template<typename X, typename Y, typename ...Z, typename Dispatch>
VULKAN_HPP_INLINE StructureChain<X, Y, Z...> PhysicalDevice::getFormatProperties2( VULKAN_HPP_NAMESPACE::Format format, Dispatch const &d ) const VULKAN_HPP_NOEXCEPT
{
StructureChain<X, Y, Z...> structureChain;
VULKAN_HPP_NAMESPACE::FormatProperties2& formatProperties = structureChain.template get<VULKAN_HPP_NAMESPACE::FormatProperties2>(); //This line
d.vkGetPhysicalDeviceFormatProperties2( m_physicalDevice, static_cast<VkFormat>( format ), reinterpret_cast<VkFormatProperties2*>( &formatProperties ) );
return structureChain;
}
Run Code Online (Sandbox Code Playgroud)
谁能帮我弄清楚这行的意思?
以下代码有什么问题:
Point2D.h
template <class T>
class Point2D
{
private:
T x;
T y;
...
};
Run Code Online (Sandbox Code Playgroud)
PointsList.h
template <class T>
class Point2D;
template <class T>
struct TPointsList
{
typedef std::vector <Point2D <T> > Type;
};
template <class T>
class PointsList
{
private:
TPointsList <T>::Type points; //Compiler error
...
};
Run Code Online (Sandbox Code Playgroud)
我想创建没有直接类型规范的新用户类型TPointsList ...
我正在研究不再存在的其他代码,它是旧的CodeWarrior代码.XCode抱怨这个:
template <class listClass,class itemClass>
void FxStreamingObjectList<listClass,itemClass>::StreamOut(FxStream *stream)
{
if (listClass::size())
{
stream->PutSeparator();
stream->PutString(mFieldName.c_str());
stream->PutSeparator();
stream->PutBeginList();
stream->Indent(+1);
listClass::iterator iter;
for (iter=listClass::begin(); iter != listClass::end(); iter++)
{
stream->PutSeparator();
stream->PutString( (*iter)->GetClassID() );
}
(*iter)->StreamOut(stream);
}
stream->Indent(-1);
stream->PutSeparator();
stream->PutEndList();
stream->PutSeparator();
}
Run Code Online (Sandbox Code Playgroud)
}
我得到错误listClass::iterator iter;,for (iter=listClass::begin(); iter != listClass::end(); iter++)那是:
error: expected `;' before 'iter'
error: 'iter' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
同一.h中的其他地方,相同类型的模板声明我得到的错误如下:
error: dependent-name 'listClass::iterator' is parsed as a non-type, but instantiation yields a type
Run Code Online (Sandbox Code Playgroud)
在:
for (listClass::iterator …
我上了一堂课:
template <typename T>
class btree {
public:
btree(size_t maxNodeElems);
~btree() {}
struct node {
list <T> elements;
node *lvl;
};
private:
size_t maxNodeElems;
node* root;
};
template <typename T>
btree<T>::btree(size_t maxNodeElems) {
if (maxNodeElems > 0) maxNodeElems = maxNodeElems;
root = new node;
root->lvl = new node[maxNodeElems+1];
}
template <typename T>
pair <typename btree<T>::iterator, bool> btree <T>::insert (const T& elem) {
pair <btree<T>::node, bool> start;
start = addElement (elem, *root);
pair <typename btree<T>::iterator, bool> final;
return final;
}
template …Run Code Online (Sandbox Code Playgroud) 我test在类中有一个静态模板方法,A它接受一个bool模板参数.当我尝试像这样调用函数时:
x = A::test<true>(...);
Run Code Online (Sandbox Code Playgroud)
解析器抱怨,因为它将<less视为小于运算符.我怎样才能告诉编译器这是一个模板实例而不是一个oprator呢?
我有一行看起来如下:
F::enable<sizeof(value_type), offset>(index);
Run Code Online (Sandbox Code Playgroud)
我得到以下编译器错误(GCC 6.3.1):
error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘long unsigned int’ to binary ‘operator<’
Run Code Online (Sandbox Code Playgroud)
显然,左尖括号被解释为小于运算符,而不是模板参数列表的开头.F是一个模板参数(F类),我希望它包含一个带签名的静态成员函数:
template <GLsizei stride, const GLvoid* offset>
static void enable(GLuint index);
Run Code Online (Sandbox Code Playgroud)
我试过自由地插入空格,查看typedeffing函数,看看转移到常规函数(由于继承的东西不可能).我已经注释掉了这条线,看看上面是否有什么东西引起了问题,没有骰子.有没有办法向编译器解释我不想比较函数和size_t,我想在类中调用静态成员函数的特定特化?
我在模板化的类中有一个模板化的静态方法,我从模板化的函数中调用它.编译因错误而失败error: expected primary-expression before '...' token.
这是一个示例代码.它有一些未使用的模板参数,但与我的实际代码完全相同,其中这些参数很重要.
temp late<typename T>
class Run {
public:
template<typename ...Args>
static void run(Args... args) {}
};
template <typename T, typename ...Args>
void
run2(Args ...args)
{
Run<int>::run<Args...>(args...); // OK
Run<T>::run<Args...>(args...); // Fail on first . in Args...
}
int main() {
run2<int>(1, 2, 3);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
% g++ -std=gnu++11 -o try try.cc
try.cc: In function 'void run2(Args ...)':
try.cc:13:21: error: expected primary-expression before '...' token
Run<T>::run<Args...>(args...); // Fail on first . …Run Code Online (Sandbox Code Playgroud)