在这个例子中,是否可以允许推导 的模板参数类型tuple?
#include<tuple>
#include<string>
template<class T1, class T2>
void fun(std::tuple<T1, T2> t, std::string other){}
int main(){
fun(std::tuple<double, int>(2.,3), std::string("other")); // ok
fun(std::make_tuple(2.,3), std::string("other")); // ok, but trying to avoid `make_tuple`
fun({2.,3},std::string("other")); // desired syntax but
// giving compilation error: candidate template ignored: couldn't infer template argument 'T1' void fun(std::tuple<T1, T2> t)
}
Run Code Online (Sandbox Code Playgroud)
我添加了第二个参数,other以避免涉及函数级别的可变参数的解决方案fun。另外,我试图避免使用make_tuple,至少在用户代码中(即在main())。事实上,它不需要是tuple所涉及的类型,只要允许“所需的语法”,并且可以在稍后阶段以某种方式推断出其元素类型。
(另外,虽然相似,但这无关,initializer_list因为它在大括号中具有不同的元素根本不起作用)
它至少在clang 3.2和时失败gcc 4.7.2。它是否有希望与当前或不久的将来的标准兼容?(例如未来(?)initializer_tuple。)
(这对于通过聚合子元素来增加函数调用的表现力非常有用,但这可以争论)
注意:对于示例代码,似乎std::forward_as_tuple …
我在一个正在研究的项目中得到了类似下面代码的东西.我觉得我被允许这样做真的很奇怪,但是现在我开始想知道我最有可能是一个建筑师,这让我想到了这一点.
我的问题是:
这是我的界面:
namespace ThisAndThat
{
public interface ICanDoThis
{
string Do();
}
public interface ICanDoThat
{
string Do();
}
public interface ICanDoThisAndThat : ICanDoThis, ICanDoThat
{
new string Do();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的具体课程:
namespace ThisAndThat
{
public class CanDoThisAndThat : ICanDoThisAndThat
{
public string Do()
{
return "I Can Do This And That!";
}
string ICanDoThis.Do()
{
return "I Can Do This!";
}
string ICanDoThat.Do()
{
return "I Can Do That!";
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的通过考试:
using Xunit; …Run Code Online (Sandbox Code Playgroud) 考虑以下三个接口:
interface IBaseInterface
{
event EventHandler SomeEvent;
}
interface IInterface1 : IBaseInterface
{
...
}
interface IInterface2 : IBaseInterface
{
...
}
Run Code Online (Sandbox Code Playgroud)
现在考虑以下实现 IInterface1 和 IInterface 2 的类:
class Foo : IInterface1, IInterface2
{
event EventHandler IInterface1.SomeEvent
{
add { ... }
remove { ... }
}
event EventHandler IInterface2.SomeEvent
{
add { ... }
remove { ... }
}
}
Run Code Online (Sandbox Code Playgroud)
这会导致错误,因为 SomeEvent 不是 IInterface1 或 IInterface2 的一部分,而是 IBaseInterface 的一部分。
Foo 类如何同时实现 IInterface1 和 IInterface2?
c# implementation explicit interface explicit-implementation
我有以下类,我试图从ExportFileBaseBL类调用Compare方法,但我得到错误
无法将类型'Class1'隐式转换为'T'.存在显式转换(您是否错过了演员?)
public abstract class Class1<T> where T: Class2
{
public abstract Class1<T> Compare(Class1<T> otherObj);
}
public abstract class Class3<T, U> where T: Class1<U>
where U: Class2
{
public T Compare(T obj1, T obj2)
{
if (obj1.Prop1 > obj2.Prop1)
{
return obj1.Compare(obj2); // Compiler Error here
}
else
{
return obj2.Compare(obj1); // Compiler Error here
}
}
}
Run Code Online (Sandbox Code Playgroud)
类型转换不应该隐含吗?我错过了什么吗?
我读了一些文档,给出了与C兼容的功能的简单示例.
__declspec(dllexport) MyFunction();
Run Code Online (Sandbox Code Playgroud)
我很开心.我写了一个小应用程序使用这个dll的功能.我使用显式链接
LoadLibrary()
Run Code Online (Sandbox Code Playgroud)
功能.C风格的功能没有问题.但是当我把我的班级写成
namespace DllTest
{
class Test
{
public:
__declspec(dllexport) Test();
__declspec(dllexport) void Function( int );
__declspec(dllexport) int getBar(void);
private:
int bar;
};
}
#endif
Run Code Online (Sandbox Code Playgroud)
它编译得很好,并且创建了Dll.使用C风格函数时,我只是从LoadLibrary()和GetProcAddress(...)函数中获取函数指针.
我之前的用法是
typedef void (*Function)(int);
int main()
{
Function _Function;
HINSTANCE hInstLibrary = LoadLibrary(TEXT("test.dll"));
if (hInstLibrary)
{
_Function = (Function)GetProcAddress(hInstLibrary,"Function");
if (_Function)
{
// use the function
Run Code Online (Sandbox Code Playgroud)
但现在我不知道如何实例化我的课程?我如何使用显式链接或隐式链接?
任何有关代码示例的帮助将不胜感激.
我之前听说过,如果我没有做一些聪明的事情,比如将值用作位掩码,我应该让编译器选择为枚举常量指定哪些值.如果我只是使用枚举值来获得更明确的代码文档,那么如果我没有明确定义所有值,是否会出现任何陷阱?我相信值是按升序分配的.我应该定义第一个值以确保每个连续编译的值相同吗?
我最近在我的代码上发现了一个错误,我花了几个小时来调试.
问题出在一个定义为:
unsigned int foo(unsigned int i){
long int v[]={i-1,i,i+1} ;
.
.
.
return x ; // evaluated by the function but not essential how for this problem.
}
Run Code Online (Sandbox Code Playgroud)
v的定义在我的开发机器上没有引起任何问题(ubuntu 12.04 32位,g ++编译器),其中unsigned int被隐式转换为long int,因此负值被正确处理.
在另一台机器(ubuntu 12.04 64位,g ++编译器)上,但此操作并不安全.当i = 0时,v [0]没有设置为-1,而是设置为一些奇怪的大值(因为它经常在尝试使无符号int为负时发生).
我可以解决将i的值转换为long int的问题
long int v[]={(long int) i - 1, (long int) i, (long int) i + 1};
Run Code Online (Sandbox Code Playgroud)
一切正常(在两台机器上).
我无法弄清楚为什么第一个在机器上正常工作而在另一个上不起作用.
你能帮助我理解这一点,以便将来可以避免这个或其他问题吗?
说我声明一个模板类A中a.h
#include <iostream>
template<bool b>
class A {
public:
void print(std::ostream& out);
};
Run Code Online (Sandbox Code Playgroud)
并确定在打印方法a.cpp(与明确的instatiation true和false)
#include "a.h"
template<bool b>
void A<b>::print(std::ostream& out) {
out << "A" << b;
}
template class A<true>;
template class A<false>;
Run Code Online (Sandbox Code Playgroud)
main.cpp可以是一个主要的主要程序示例
#include "a.h"
int main() {
A<true> a;
a.print(std::cout);
}
Run Code Online (Sandbox Code Playgroud)
上面的小项目编译得很好.
问题:如果我将显式实例化放在print方法(in a.cpp)的定义之上,则代码不再编译,并且通常会undefined reference to A<true>::print(...)出现错误.
#include "a.h"
template class A<true>;
template class A<false>;
template<bool b>
void A<b>::print(std::ostream& out) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试用C++处理命名空间和模板.我可以得到以下代码在MSVC中编译(没有警告或错误),但我没有运气与CYGWIN/GCC的各种排列.任何帮助,将不胜感激.
在头文件中,我声明了一个模板化的子类,如下所示:
#include <gdal.h>
namespace sfms {
template <class _type, GDALDataType _gdal> class SmfsGrid_Typed : public SfmsGrid_Base {
public:
SmfsGrid_Typed();
SmfsGrid_Typed(const SmfsGrid_Typed<_type, _gdal> *toCopy);
SmfsGrid_Typed(std::string filename);
virtual ~SmfsGrid_Typed();
virtual bool OpenRead();
virtual bool OpenWrite();
protected:
_type m_nodata_value;
virtual SfmsGrid_Base *New() const;
virtual SfmsGrid_Base *New(SfmsGrid_Base *toCopy) const;
virtual void initCopy(SfmsGrid_Base *copy) const;
};
template SmfsGrid_Typed<double, GDT_Float64>;
template SmfsGrid_Typed<float, GDT_Float32>;
template SmfsGrid_Typed<int, GDT_Int32>;
typedef SmfsGrid_Typed<double, GDT_Float64> SmfsGrid_Double;
typedef SmfsGrid_Typed<float, GDT_Float32> SmfsGrid_Float;
typedef SmfsGrid_Typed<int, GDT_Int32> SmfsGrid_Int;
}
Run Code Online (Sandbox Code Playgroud)
在源文件中,我实例化专用模板类,如下所示:
void hi_there() {
//...
sfms::SmfsGrid_Typed<int, …Run Code Online (Sandbox Code Playgroud) 作为JI的初学者,我常常遇到隐性程序,与更熟悉的显式形式相比,它们看起来相当拜占庭.
现在只是因为我发现解释困难并不意味着默会形式是错误的或错误的.通常,默认形式比显式形式短得多,因此更容易在视觉上同时看到所有形式.
专家提问:这些隐性形式是否表达了更好的结构感,并可能提炼出潜在的计算机制?还有其他好处吗?
我希望答案是肯定的,对于一些非平凡的例子也是如此......