标签: declaration

隐藏功能模板,声明专业化

这是C++模板的后续内容:防止基本模板的实例化

我使用模板来实现函数重载而不会出现隐式类型转换:声明函数模板,定义所需的特化(重载).一切都很好,除了错误的代码在链接阶段之前不会产生错误:

lib.hpp:

template<class T> T f(T v);
Run Code Online (Sandbox Code Playgroud)

lib.cpp:

#include "lib.hpp"

template<> long f(long v) { return -v; }
template<> bool f(bool v) { return !v; }
Run Code Online (Sandbox Code Playgroud)

main.cpp中:

#include <iostream>
#include "lib.hpp"

int main()
{
    std::cout
        << f(123L) << ", "
        << f(true) << ", "
        << f(234) << "\n"
    ;
}
Run Code Online (Sandbox Code Playgroud)

gcc输出:

c++ -O2 -pipe -c main.cpp
c++ -O2 -pipe -c lib.cpp
c++ main.o lib.o -o main
main.o(.text+0x94): In function `main':
: undefined reference …
Run Code Online (Sandbox Code Playgroud)

c++ templates declaration template-specialization

2
推荐指数
1
解决办法
2058
查看次数

这两个java变量声明有什么区别?

public class SomeClass {
    private HashSet<SomeObject> contents = new HashSet<SomeObject>();
    private Set<SomeObject> contents2 = new HashSet<SomeObject>();
}
Run Code Online (Sandbox Code Playgroud)

有什么不同?最后他们都是HashSet不是吗?第二个看起来对我来说是错的,但我已经看到它经常使用,接受和工作.

java variables declaration set hashset

2
推荐指数
1
解决办法
490
查看次数

order initializiation静态参数

是否有任何关于静态只读参数初始化顺序的规范?

在下面的例子中,可以确定,数组总是以6的长度创建吗?

public class Foo {
    private static readonly int MAX_STACKSIZE = 6; 
    private static readonly int[] m_stack = new int[MAX_STACKSIZE]; 
}
Run Code Online (Sandbox Code Playgroud)

或者有没有机会在MAX_STACKSIZE 之前初始化m_stack?

@Edit:将const更改为静态只读

c# specifications initialization declaration

2
推荐指数
1
解决办法
101
查看次数

为什么这种语法错了?不能将GET和SET用于行中的属性

我必须定义很多字段,我不能这样做:

public double d1, d2, d3, d4, d5, d6 {get;set;} //does not work
Run Code Online (Sandbox Code Playgroud)

相反,我必须这样做

public double d1 { get; set; } public double d2 { get; set; } //and so on
Run Code Online (Sandbox Code Playgroud)

但在私人领域我们有:

private double d1, d2, d3, d4 = 0; //Works
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么它不起作用.也许somone可以解释一下吗?有办法吗?

更新: 似乎很多复制粘贴让我搞砸了我的知识,忘记了Fields&Properties之间的区别.无论如何在第一行我的意思是一切应该是属性

c# field declaration

2
推荐指数
2
解决办法
164
查看次数

double&(不通过引用传递)C++

typedef double real8;

typedef real8 Real_t;

Real_t& x(int y);
Run Code Online (Sandbox Code Playgroud)

什么是Real_t&?? 我只看到一个数据类型后跟"&"表示通过引用传递.但这是在申报行中.这是什么意思 ?

c++ double reference declaration

2
推荐指数
2
解决办法
1284
查看次数

如何在Lua中声明变量而不将其赋予nil?

我发现做以下事情非常有用:

if not variable then
    variable = value
end
Run Code Online (Sandbox Code Playgroud)

当然,我通常认为该变量是本地变量,但我不能在我的if中声明它本地,或者它不可访问.

所以我有时会这样做:

local variable
if not variable then
    variable = value
end
Run Code Online (Sandbox Code Playgroud)

问题是,当我迭代这个代码时,变量声明将变量设置为nil.如果我能够拥有一个全局值(我可以),我可以通过不在if块之外声明变量来绕过它.

但是,有没有什么方法可以让我既有本地价值又让它保持价值?

variables lua scope declaration local

2
推荐指数
1
解决办法
6782
查看次数

在Java中声明一个嵌套类

我对子类有点困惑.这是我的代码:

public class MedHistory {

    private String grafts;
    private String allergies;
    private String diseases;
    private String surgeries;
    private String medicalTreatment;

    //Constructors (#2)

    public MedHistory(String allergies, String diseases, String grafts,
            String treatments, String surgeries) {
        this.allergies=allergies;
        this.diseases=diseases;
        this.grafts=grafts;
        this.medicalTreatment=treatments;
        this.surgeries=surgeries;
    }

    public MedHistory() {
        this.allergies="";
        this.diseases="";
        this.grafts="";
        this.medicalTreatment="";
        this.surgeries="";
    }

    //Getters 
    public String getGrafts() {
        return grafts;
    }

    public String getAllergies() {
        return allergies;
    }

    public String getDiseases() {
        return diseases;
    }

    public String getSurgeries() {
        return surgeries;
    }

    public String …
Run Code Online (Sandbox Code Playgroud)

java inheritance declaration nested-class

2
推荐指数
1
解决办法
224
查看次数

在for循环中声明变量不好?

for $i (1..1000000)
{
    my $a = rand(10);
    my $b = rand(10);
    my $c = rand(10);
}
Run Code Online (Sandbox Code Playgroud)

将上面的代码分配用于新的存储位置$a,$b和$c循环的每次迭代期间,从而使用起来更内存比下面的代码?

my ($a,$b,$c);
for $i (1..1000000)
{
    $a = rand(10);
    $b = rand(10);
    $c = rand(10);
}
Run Code Online (Sandbox Code Playgroud)

perl memory-leaks declaration

2
推荐指数
1
解决办法
72
查看次数

为什么"静态可变int n"; 在C++中不允许?

struct A
{
    // clang 3.8 error : cannot combine with previous 'static' declaration specifier
    static mutable int n;
};
Run Code Online (Sandbox Code Playgroud)

我认为static mutable int n;在某些情况下有明确的语义.为什么在C++中不允许这样做?

更新:

另一个例子显示clear semantics:

class SharedValue
{
public:
    void Set(int n)
    {
        std::lock_guard lock(_mtx);
        _n = n;
    }


    int Get() const
    {
        std::lock_guard lock(_mtx);
        //
        // _mtx should be mutable, 
        // because this is in const member function
        //

        return _n;
    }

private:
    static mutable std::mutex _mtx;
    int _n;
};
Run Code Online (Sandbox Code Playgroud)

c++ static declaration mutable semantics

2
推荐指数
1
解决办法
385
查看次数

转到另一个包中的函数定义

我读这个岗位约time.startTimer声明和定义。

从答案中,time.startTimer的声明src/time/sleep.go 如下:

func startTimer(*runtimeTimer)
Run Code Online (Sandbox Code Playgroud)

其定义src/runtime/time.go如下:

func startTimer(t *timer) {
    if raceenabled {
        racerelease(unsafe.Pointer(t))
    }
    addtimer(t)
}
Run Code Online (Sandbox Code Playgroud)

因此,似乎可以在一个.go文件中声明一个函数,然后在另一个.go文件中实现该函数。我尝试了相同的方法,例如,在a.go中声明了一个函数并在b.go中实现了该函数,但是当时,它总是失败go run a.go。这是正确的方法吗?如何声明在另一个.go文件中实现的功能?或中没有import任何内容。Go如何做到?sleep.gotime.go

谢谢

declaration function go

2
推荐指数
1
解决办法
151
查看次数