我不确定这是否是编译器错误,还是不确定我是否做了违反标准的操作而导致未定义的行为。这是我的代码:
#include <iostream>
template<auto InputSize, typename SizeType = decltype(InputSize)>
class StaticArray
{
public:
using size_type = SizeType;
using size_type2 = decltype(InputSize);
};
int main()
{
//StaticArray<2, int> s1;
StaticArray<2ull, int> s3;
std::cout << typeid(decltype(s3)::size_type).name() << "\t" << typeid(decltype(s3)::size_type2).name() << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果注释掉的行仍然被注释掉,我将得到正确的输出:int unsigned __int64
。但是,如果取消注释该行,则会得到输出int int
。作为参考,我正在MSVC 2017 v15.9.2的x86调试中对此进行编译。
我目前正在学习STL,我对find和const迭代器有一些不确定性.假设我有一个find函数:
some_stl_container::const_iterator found = myContainer.find(value);
Run Code Online (Sandbox Code Playgroud)
在那之后,我应该检查我得到的found
另一个const_iterator,或者只是对一个迭代器进行检查是有效的.基本上这样做会有任何区别:
if(found!=myContainer.cend())
Run Code Online (Sandbox Code Playgroud)
还有这个:
if(found!=myContainer.end())
Run Code Online (Sandbox Code Playgroud)
第一个看起来更准确(至少对我而言),但第二个应该也能正常工作,对吗?
我正在尝试根据作为参数传递给它的lambda的优缺点来专门化模板化函数。这是我想出的解决方案:
template<typename Function, bool>
struct helper;
template<typename Function>
struct helper<Function, false>
{
auto operator()(Function&& func)
{
std::cout << "Called 2 argument version.\n";
return func(1, 2);
}
};
template<typename Function>
struct helper<Function, true>
{
auto operator()(Function&& func)
{
std::cout << "Called 3 argument version.\n";
return func(1, 2, 3);
}
};
template<typename T>
struct B
{
T a;
const T someVal() const { return a; }
};
template<typename Function, typename T>
auto higherOrderFun(Function&& func, const T& a)
{
return helper<Function, std::is_invocable<Function, decltype(a.someVal()), …
Run Code Online (Sandbox Code Playgroud) 我有几个我想为CRTP基类的派生类工作的函数。问题是,如果我将派生类传递给用于CRTP类的自由函数,则会产生歧义。下面的代码是一个说明这个问题的最小示例:
template<typename T>
struct A{};
struct C : public A<C>{};
struct B{};
template<typename T, typename U>
void fn(const A<T>& a, const A<U>& b)
{
std::cout << "LT, RT\n";
}
template<typename T, typename U>
void fn(const T a, const A<U>& b)
{
std::cout << "L, RT\n";
}
template<typename T, typename U>
void fn(const A<T>& a, const U& b)
{
std::cout << "LT, R\n";
}
int main()
{
C a; // if we change C to A<C> everything works fine
B b; …
Run Code Online (Sandbox Code Playgroud) std::conjunction
在对我有关不短路(连接模板不短路)的问题的评论中,有人建议我std::is_invocable_r
作为解决我的问题的方法。然而,当我尝试使用它时,我发现了一些非常奇怪的行为。例如,此代码使两个断言均失败:
#include <type_traits>
int main()
{
static_assert(!std::is_invocable_r_v<void, int(int), int>);
static_assert(std::is_convertible_v<int,void>);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
is_invocable_r
请注意cppreference的描述:
确定是否
Fn
可以使用参数调用ArgTypes...
以产生可转换为 的结果R
。
显然int
不可转换为void
,第二个断言证实了这一点。问题是为什么会std::is_invocable_r_v<void, int(int), int>
产生一个true
值。这是一个实例: https: //godbolt.org/z/HywH7D
请注意,可以std::is_void_v<std::invoke_result_t<int(int),int>>
在此处获取正确答案(https://godbolt.org/z/YMvc47),但这不是我的问题。(它并不能解决我的连词问题)。
有什么语法可以在编译时定义静态数组的维数?假设我有一个大小为D的元组,值是d_0,...,d_{D-1}
。我希望能够创建一个数组T arr[d_0][d_1]...[d_{D-1}]
。有什么办法可以在编译时实现?我专门问的是静态数组语法,而不是关于如何嵌套结构。
这是一个代码片段,以阐明我想要实现的目标:
template<typename T, template<typename, auto> typename Container, auto DimC, auto...Dim>
struct NestedContainer
{
using type = Container<typename NestedContainer<T, Container, Dim...>::type, DimC>;
};
template<typename T, template<typename, auto> typename Container, auto Dim>
struct NestedContainer<T, Container, Dim>
{
using type = Container<T, Dim>;
};
template<typename T, int D>
struct Arr
{
T e[D];
T& operator[](int i) { return e[i]; }
};
template<typename T, int D, int...Dim>
struct MultiArr
{
using multi_arr = typename NestedContainer<T, Arr, Dim...>::type;
multi_arr …
Run Code Online (Sandbox Code Playgroud) 我有一个带有以下签名的函数:
template<typename Container, auto First = 0, auto Last = Container::size()>
doSomething(const Container& containter){...}
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以允许对模板参数进行重新排序,所以我可以像下面这样调用函数:
doSomething<3,5>(someContainer);
Run Code Online (Sandbox Code Playgroud)
不必这样做:
doSomething<decltype(someContainer), 3,5>(someContainer);
Run Code Online (Sandbox Code Playgroud)
这不会是一个问题,如果我能够继续前进someCountainer
后Last
,但默认值Last
源自Container
。有没有一种方法可以Container
在模板参数列表中转发声明,或者有什么功能可以使我避免decltype(...)
每次都添加?
我不知道这是否可行,但我想"隐藏"给定类的一些模板参数.这就是我的意思,说我有以下代码:
template<class A, int b>
class Foo
{
};
template<template<class,int> class Foo_specialized, class A, int b>
class Bar
{
Foo_specialized<A,b> obj;
};
Run Code Online (Sandbox Code Playgroud)
现在假设Bar不需要知道A,但需要了解b.当然这样的事情是完美的(以下是伪代码,只是为了说明这个想法):
template<template<int> class Foo_specialized_by_first_parameter, int b>
class Bar
{
Foo_specialized_by_first_parameter<b> obj;
};
Run Code Online (Sandbox Code Playgroud)
我不确定这是否有可能,这个想法是在实例化Bar时有这样的东西:
Bar<Foo<float>, 5> bar_instance;
Run Code Online (Sandbox Code Playgroud)
当然这不起作用,因为Foo不接受1个参数.基本上我需要一些(Foo<float>)<5>
可能的东西.我能想到的最接近的事情是在哈斯克尔中讨论.
如何为成员函数正确调用 invoke_result?或者专门用于操作员成员函数。我试过std::invoke_result<T::operator[], size_type>
没有成功。在这种情况下,正确的语法是什么?
我是C++的新手,我目前正在玩模板以更好地理解它们.这是我一直在尝试的:
#include <iostream>
#include <typeinfo>
using namespace std;
template <typename T>
class someContainer
{
private:
T val1;
T val2;
public:
someContainer(const T& in1, const T& in2)
:val1(in1), val2(in2) {}
template <template <typename Ty> class Comp>
void sort()
{
bool result = Comp<T>()(val1, val2);
cout << result << endl;
return;
}
};
template <typename R>
class Compare
{
public:
bool operator () (const R& a, const R& b)
{
return a>b;
}
};
int main()
{
someContainer<int> myCont(7,6);
myCont.sort<Compare>();
cin.ignore();
return …
Run Code Online (Sandbox Code Playgroud)